I have a csv
file like blew:
word tag
w1 t1
w2 t2
w3 t3
w4 t4
w5 t5
w6 t6
w7 t7
w8 t8
w9 t9
That I want to add a column named sentence number and How to value sentences shown in below.
Desired output:
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
When we reach a blank row, one will be added to the previous value. I want something like this. How to reach to my desired output above?
Code:
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)