[英]When reading a text file, why do I get b' in front of each line?
Not sure what's going on here. 不知道这是怎么回事。 I have a text file with the following contents: 我有一个包含以下内容的文本文件:
1999,3,Rep Armey, Richard K.,[TX-26],0
1999,4,Rep Armey, Richard K.,[TX-26],0
1999,5,Rep Armey, Richard K.,[TX-26],0
1999,6,Rep Hyde, Henry J.,[IL-6],0
1999,7,Rep Talent, Jim,[MO-2],11,Rep DeMint, Jim,[SC-4],...
...
then I read it in using this code in python 3.3: 然后我在python 3.3中使用以下代码阅读了它:
with open(bill_file_name,'rb') as input_file:
lines=input_file.readlines()
for line in lines:
line_list=str(line).split(',')
session=line_list[0]
when I do print(session)
I get b'1999
for all of the entries. 当我执行print(session)
,所有条目都得到b'1999
。
What's going on? 这是怎么回事? How do I avoid this? 如何避免这种情况?
This is probably because you opened the file in binary mode. 这可能是因为您以二进制模式打开了文件。
Try open(bill_file_name,'r')
尝试open(bill_file_name,'r')
That's because you have the b
(binary) option enabled while reading. 这是因为您在阅读时启用了b
(二进制)选项。 Use the following. 使用以下内容。
with open('a.txt', 'r') as inp:
lines = inp.readlines()
for line in lines:
line_list=str(line).split(',')
session=line_list[0]
>>> print(session)
1999
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.