簡體   English   中英

類型錯誤:預期的字符串或緩沖區

[英]TypeError: expected string or buffer

我有這個簡單的代碼:

import re, sys

f = open('findallEX.txt', 'r')
lines = f.readlines()
match = re.findall('[A-Z]+', lines)
print match

我不知道為什么我收到錯誤:

'預期的字符串或緩沖區'

任何人都可以幫忙嗎?

lines是一個列表。 re.findall()不接受列表。

>>> import re
>>> f = open('README.md', 'r')
>>> lines = f.readlines()
>>> match = re.findall('[A-Z]+', lines)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python2.7/re.py", line 177, in findall
    return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer
>>> type(lines)
<type 'list'>

來自help(file.readlines) readlines()用於循環/迭代:

readlines(...)
    readlines([size]) -> list of strings, each a line from the file.

要查找文件中的所有大寫字符:

>>> import re
>>> re.findall('[A-Z]+', open('README.md', 'r').read())
['S', 'E', 'A', 'P', 'S', 'I', 'R', 'C', 'I', 'A', 'P', 'O', 'G', 'P', 'P', 'T', 'V', 'W', 'V', 'D', 'A', 'L', 'U', 'O', 'I', 'L', 'P', 'A', 'D', 'V', 'S', 'M', 'S', 'L', 'I', 'D', 'V', 'S', 'M', 'A', 'P', 'T', 'P', 'Y', 'C', 'M', 'V', 'Y', 'C', 'M', 'R', 'R', 'B', 'P', 'M', 'L', 'F', 'D', 'W', 'V', 'C', 'X', 'S']

lines是一個字符串列表, re.findall不適用於它。 嘗試:

import re, sys

f = open('findallEX.txt', 'r')
lines = f.read()
match = re.findall('[A-Z]+', lines)
print match

readlines()將返回文件中所有lines的列表,所以lines是一個列表。 你可能想要這樣的東西:

for line in f.readlines(): # Iterates through every line and looks for a match
#or
#for line in f:
    match = re.findall('[A-Z]+', line)
    print match

或者,如果文件不是太大,您可以將其作為單個字符串抓取:

lines = f.read() # Warning: reads the FULL FILE into memory. This can be bad.
match = re.findall('[A-Z]+', lines)
print match

片段中的“行”術語由一組字符串組成。

 lines = f.readlines()
 match = re.findall('[A-Z]+', lines)

您不能將整行發送到re.findall('pattern',<string>)

您可以嘗試逐行發送

 for i in lines:
  match = re.findall('[A-Z]+', i)
  print match

或將整個行集合轉換為單行(每行以空格分隔)

 NEW_LIST=' '.join(lines)
 match=re.findall('[A-Z]+' ,NEW_LIST)
 print match

這可能會幫助你

re.findall 在字符串中查找所有出現的正則表達式,並在列表中返回。 在這里,您使用的是字符串列表,您需要使用它來使用 re.findall

注 - 如果正則表達式失敗,則返回一個空列表。

import re, sys

f = open('picklee', 'r')
lines = f.readlines()  
regex = re.compile(r'[A-Z]+')
for line in lines:
     print (re.findall(regex, line))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM