2011-04-11 22 views
22

İkili veri ve metin verilerini karıştıran bir dosyam var. Ben normal ifade yoluyla ayrıştırmak istiyorum ama bu hatayı alıyorum:Normal ifade ikili dosya ayrıştırılıyor mu?

TypeError: can't use a string pattern on a bytes-like object 

O mesajı tahmin ediyorum Python ikili dosyaları ayrıştırmak istemiyor demektir. Dosyayı "rb" bayraklarıyla açıyorum.

Python'da ikili dosyaları normal ifadelerle nasıl ayrıştırabilirim?

DÜZENLEME: Python 3.2.0

+0

Ben bu doğru, Python 3 kullandığınız bayt benzeri nesneye referanstan tahmin ediyorum? –

cevap

16

Sanırım Python 3'ü kullanıyorsunuz.

1.Opening a file in binary mode is simple but subtle. The only difference from opening it in text mode is that the mode parameter contains a 'b' character.

........

4.Here’s one difference, though: a binary stream object has no encoding attribute. That makes sense, right? You’re reading (or writing) bytes, not strings, so there’s no conversion for Python to do.

http://www.diveintopython3.net/files.html#read

Sonra Python 3'te, bir dosyadan ikili akışı yana bayt akışı, bir normal ifade, bir dosyadan akışını analiz etmek için bir bit dizisi, bir charcaters sekansı ile tanımlanmalıdır.

In Python 2, a string was an array of bytes whose character encoding was tracked separately. If you wanted Python 2 to keep track of the character encoding, you had to use a Unicode string (u'') instead. But in Python 3, a string is always what Python 2 called a Unicode string — that is, an array of Unicode characters (of possibly varying byte lengths).

http://www.diveintopython3.net/case-study-porting-chardet-to-python-3.html

ve

In Python 3, all strings are sequences of Unicode characters. There is no such thing as a Python string encoded in UTF-8, or a Python string encoded as CP-1252. “Is this string UTF-8?” is an invalid question. UTF-8 is a way of encoding characters as a sequence of bytes. If you want to take a string and turn it into a sequence of bytes in a particular character encoding, Python 3 can help you with that.

http://www.diveintopython3.net/strings.html#boring-stuff

ve

4.6. Strings vs. Bytes# Bytes are bytes; characters are an abstraction. An immutable sequence of Unicode characters is called a string. An immutable sequence of numbers-between-0-and-255 is called a bytes object.

....

1.To define a bytes object, use the b' ' “byte literal” syntax. Each byte within the byte literal can be an ASCII character or an encoded hexadecimal number from \x00 to \xff (0–255).

http://www.diveintopython3.net/strings.html#boring-stuff

pat = re.compile(b'[a-f]+\d+') 

ve aşağıdaki gibi Yani regex tanımlayacak değil

olarak Burada
pat = re.compile('[a-f]+\d+') 

Daha açıklamalar:

15.6.4. Can’t use a string pattern on a bytes-like object

+0

Gelecekte başvurmak için _why_ açıkladığından, yukarıda belirtilenler belirtildi. Bir kodlamanın ne olduğunu biliyorum, ve yazının çok özlü, imho olsa da, sonunda ihtiyacım olan cevabı verirsin. – DonkeyMaster

+0

Bir ipucu alın! -) –

+1

@John Machin Ne demek istiyorsun, lütfen? – eyquem

-2

Bu ise piton 2,6

>>> import re 
>>> r = re.compile(".*(ELF).*") 
>>> f = open("/bin/ls") 
>>> x = f.readline() 
>>> r.match(x).groups() 
('ELF',) 
+0

Bu kod 'import re; r = re.compile ("(Bu)"); f = open (r "C: \ WINDOWS \ system32 \ mspaint.exe", "rb"); x = f.readline(); r.match (x) .groups() ' , – DonkeyMaster

24

için benim için çalışıyor kullanıyorum senin re.compile burada, başlangıç ​​ile ifade eden bir bytes nesne kullanmak gerekir b:

r = re.compile(b"(This)") 

Bu

dize arasındaki fark konusunda seçici olmak Python 3'tür s ve baytlar.

+1

numaralı özgün mesajımla aynı hatayı döndürür. Bu cevap beni doğru yola koydu, çok teşekkürler. – DonkeyMaster