[英]Python : split text to list of lines
我是 Python 新手,但我有文本文件,例如:
12345 | 6789 | abcd | efgh
我希望我的输出像:
12345
6789
abcd
efgh
======================
我真的不知道脚本,但我通过这些函数 split() , strip() 制作了很多脚本,归咎于责备
但我没能做到,所以有人可以寻求帮助。
我将不胜感激任何帮助。
with open('contacts_index1.txt') as f:
lines = f.read().splitlines("|")
从您的所有评论来看,问题似乎与文件中的实际文本有关,而不是解析它的能力。 看起来这里每个人的解决方案都在正确的轨道上,您只需要强制编码。
您所描述的错误在另一篇 StackOverflow 帖子中有所描述。
with open('contacts_index1.txt', 'r') as f:
lines = f.read().encode("utf-8").replace("|", "\n")
编辑:问题似乎是一个令人讨厌的字符没有正确解码。 使用open
你可以告诉它忽略它无法解码的字符。
import io
with io.open("contacts_index1.txt", errors="ignore") as f:
lines = f.read()replace("|", "\n")
您将不得不使用解码。 以下代码将起作用:
def dataFunction(filename):
with open(filename, encoding="utf8") as f:
return f.read()
使用文件名作为参数调用此函数:
Contents = dataFunction(filename)
elements = Contents.split("|")
for element in elements:
print(element)
您发布的代码存在一些问题:
f.read
不读取整行。 它应该是f.readline()
。splitlines
线的功能是什么?你的问题在不同方面都不清楚。 也许这个片段可能会有所帮助:
for line in open('contacts_index1.txt'):
elements = line.split('|')
for element in elements:
print element.strip()
编辑:我不知道函数splitlines
。 刚查了一下。 无论如何,您在代码中使用它的方式似乎都不正确。
我强烈建议将 csv 模块用于此类任务,因为它看起来像一个 csv 类型的文件,使用“|” 作为分隔符:
import csv
with open('contacts_index1.txt','r') as f:
reader=csv.reader(f,delimiter='|')
for row in reader:
#do things with each line
print "\n".join(row)
请逐行执行此操作。 无需一次读取整个文件。
就像是:
with open(file_name) as f_in:
for line in f_in:
for word in line.split('|'):
print word.strip()
如果是 unicode 问题,大多数情况下它是自动的:
$ cat /tmp/so.txt
12345 | 6789 | abcd | éfgh
(注意文件中的é
)
上面的程序有效。 如果它不起作用,请使用编解码器:
with open(fn) as f_in:
for line in f_in:
line=line.decode('utf-8') # or whatever codec is used for that file...
for word in line.split('|'):
print word.strip()
使用Python3,只需在打开文件时设置编码:
with open(fn, encoding='utf-8') as f_in: # <= replace with the encoding of the file...
for line in f_in:
for word in line.split('|'):
print(word.strip())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.