我在阅读csv文件时遇到问题。
csv格式: 以下是csv文件中两个条目的格式:
"1", "one", "<long class=\"like\" >\
<short class=\"over\">\
</short>
</long>", "3", "<long class=\"like\" >\
<short class=\"over\">\
</short>
</long>", "facebook"
"2", "two", "<long class=\"like\" >\
<short class=\"over\">\
</short>
</long>", "3", "<long class=\"like\" >\
<short class=\"over\">\
</short>
</long>", "facebook"
如何读取此类csv文件中的每一行?
答案 0 :(得分:1)
为什么不使用csv
套餐?
您可以阅读每一行并按照您的需要进行播放,例如:
import csv
with open('prueba.csv','r') as file:
reader = csv.reader(file, delimiter=';')
for row in reader:
<That you want to do with each row>
但也许你想做另一件事。
答案 1 :(得分:0)
假设 csv 文件中的两个条目如下所示:
"1", "one", "<long class=\"like\" ><short class=\"over\"></short></long>", "3", "<long class=\"like\" ><short class=\"over\"></short></long>" "facebook"
"2", "two", "<long class=\"like\" ><short class=\"over\"></short></long>", "3", "<long class=\"like\" ><short class=\"over\"></short></long>" "facebook"
考虑使用re.findall()
函数:
import re
with open('test.csv', 'r') as fh:
lines = fh.read().split('\n')
for l in lines:
fields = re.findall(r'^\"(\d+)\", \"(\w+)\", (.+)', l, re.S)
a, b, c = fields[0] # unpacking fields
print(a, b, c, sep='\t')
输出:
1 one "<long class=\"like\" ><short class=\"over\"></short></long>", "3", "<long class=\"like\" ><short class=\"over\"></short></long>" "facebook"
2 two "<long class=\"like\" ><short class=\"over\"></short></long>", "3", "<long class=\"like\" ><short class=\"over\"></short></long>" "facebook"