SORU
1 HAZİRAN 2009, PAZARTESİ


Utf-8 python dosyasına yazma

Gerçekten codecs.open function ile kafam karıştı. Ben ne zaman:

file = codecs.open("temp", "w", "utf-8")
file.write(codecs.BOM_UTF8)
file.close()

Bana hata veriyor

UnicodeDecodeError: 'ascii' Kodek pozisyonda bayt 0xef çözebilir mi 0: range(128) sıra

Eğer yaparsam:

file = open("temp", "w")
file.write(codecs.BOM_UTF8)
file.close()

Gayet iyi çalışıyor.

Soruneden ilk yöntem başarısız mı? Ve nasıl bom Ekle?

Eğer İkinci yöntem bunu yapmanın doğru yolu ise, ne codecs.open(filename, "w", "utf-8") kullanarak nokta?

CEVAP
1 HAZİRAN 2009, PAZARTESİ


Aşağıdaki bilgileri okuyun: http://docs.python.org/library/codecs.html#module-encodings.utf_8_sig

Bunu yapmak

with codecs.open("test_output", "w", "utf-8-sig") as temp:
    temp.write("hi mom\n")
    temp.write(u"This has ♭")

Sonuç dosyası, beklenen BOM ile UTF-8.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Avast

    Avast

    27 NİSAN 2006
  • Bigapplemagic

    Bigapplemagi

    22 EYLÜL 2011
  • EmmightySofia

    EmmightySofi

    25 EYLÜL 2011