【发布时间】:2020-08-07 20:54:31
【问题描述】:
我有一个类似自爆的csv 文件:
word tag
w1 t1
w2 t2
w3 t3
w4 t4
w5 t5
w6 t6
w7 t7
w8 t8
w9 t9
我想添加一个名为句子编号的列和如何对下面显示的句子进行赋值。
期望的输出:
sentence# word tag
sentence:1 w1 t1
w2 t2
w3 t3
sentence:2 w4 t4
w5 t5
w6 t6
w7 t7
sentence:3 w8 t8
w9 t9
当我们到达一个空白行时,将在前一个值上添加一个。我想要这样的东西。如何达到上面我想要的输出?
代码:
from csv import reader
i = 0
with open('username.csv', 'rt', encoding='utf-8') as f:
csv_reader = pd.read_csv(f, delimiter=';')
csv_reader1 = reader(f)
for line in csv_reader1:
if not line:
i+=1 # empty lines
else:
csv_reader["sentence#"] = i
print(line)
【问题讨论】:
-
欢迎来到 SO!请花点时间阅读有关如何发布熊猫问题的信息:stackoverflow.com/questions/20109391/…
标签: python-3.x pandas csv