[英]expected string or buffer in python
I have this: 我有这个:
fin = open(blah)
fin_lower= fin.readlines()
lines = [fin_lower.lower() for line in fin]
lines = line.split()
It gives: 它给:
TypeError: expected string or buffer
Is it wrong to readlines? readlines是不对的?
readlines returns a list containing all lines of data, it looks like you have a bug, and you probably want to do this: readlines返回一个包含所有数据行的列表,看起来你有一个bug,你可能想要这样做:
lines = [line.lower() for line in fin_lower]
Your code is also mixing variables around, take a good step through it, what are you trying to accomplish? 你的代码也在混合变量,通过它,你想要完成什么? You seem to mix line
and lines
a bunch. 你似乎把line
和lines
混合在一起。
re.sub
expects a string as the third argument, you gave it lines
which is a list. re.sub
需要一个字符串作为第三个参数,你给它lines
是列表。 Also, you're iterating over fin
after consuming all lines of it with readlines
. 此外,在使用readlines
消耗掉所有行之后,你正在迭代fin
。 You seem to be trying to do: 你似乎想要这样做:
with open(blah) as fin:
lines = [line.lower().replace(',', '').split() for line in fin]
Also note that you don't need re
to do a literal replacement. 另请注意,您无需re
进行文字替换。
I agree with Bartek 我同意巴特克的观点
I was able to get this done. 我能够完成这件事。
import os
import signal
import time
import sys
import re
import string
fin = open('blah','r')
fin_lower= fin.readlines()
lines=""
for line in fin_lower:
lines += line.lower()
line = re.sub(';',' ',lines)
lines = line.split()
print lines
Initial Contents of File blah 文件的初始内容等等
VISHAL; VISHAL; KHIALANI; KHIALANI; NONOE; NONOE; CANGETITDONE; CANGETITDONE;
Final Output 最终产出
['vishal', 'khialani', 'nonoe', 'cangetitdone'] ['vishal','khialani','nonoe','cangetitdone']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.