SORU
29 Mart 2011, Salı


Eğer data: '' tampon desteklemiyor arayüzü str

string = input("Please enter the text you want to compress")
file = input("Please enter the desired filename")
with gzip.open(file ".gz","wb") as f_out:
    f_out.write(string) 

Yukarıda python kodu aşağıdaki hata veriyor bana:

Traceback (most recent call last):
  File "C:/Users/Ankur Gupta/Desktop/Python_works/gzip_work1.py", line 33, in <module>
    compress_string()
  File "C:/Users/Ankur Gupta/Desktop/Python_works/gzip_work1.py", line 15, in compress_string
    f_out.write(string)
  File "C:\Python32\lib\gzip.py", line 312, in write
    self.crc = zlib.crc32(data, self.crc) & 0xffffffff
TypeError: 'str' does not support the buffer interface

CEVAP
29 Mart 2011, Salı


Eğer Python3x kullanıyorsanız string Python 2 için aynı tip değildir.x, bayt (kodlamak) dönüştürmelisiniz.

s = input("Please enter the text you want to compress")
fn = input("Please enter the desired filename")
with gzip.open(fn ".gz","wb") as f_out:
    f_out.write(bytes(s, 'UTF-8'))

Ayrıca bu modül veya fonksiyon isimleri ise string file gibi değişken adları kullanmayın.

@Tom DÜZENLEYİN

Evet, ASCII olmayan metin/sıkıştırılmış sıkıştırılmış. UTF-8 kodlaması ile Lehçe harfler kullanın:

s = 'Polish text: ąćęłńóśźżĄĆĘŁŃÓŚŹŻ'
fn = 'fn.gz'
with gzip.open(fn, 'wb') as f_out:
    f_out.write(bytes(s, 'UTF-8'))
with gzip.open(fn, 'r') as f_in:
    s2 = f_in.read().decode('UTF-8')
print(s2)

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • HSmasteryoda .

    HSmasteryoda

    22 Ocak 2010
  • Manuel Vizcaino

    Manuel Vizca

    27 Mayıs 2008
  • UKF Dubstep

    UKF Dubstep

    29 NİSAN 2009