I have the following list having the structure [(int, [], []), (int, [], []), (int, [], []), ..., (int, [], [])]. [] represents tokens of a sentence.
data = [(11.221, ['Maruyama', '(', 'Japan', ')'], ['S-PER', 'O', 'S-LOC', 'O']),
(5.56, ['MANAMA', '1996-08-22'], ['S-LOC', 'O']),
(5.381, ['BEIJING', '1996-08-22'], ['S-LOC', 'O'])]
I want to write data into a CSV file as follows:
11.221, Maruyama (Japan) , Maruyama , S-PER
(, , O
Japan, , S-LOC
), , O
[HERE SHOULD BE SPACE]
5.56 , MANAMA 1996-08-22 , MANAMA , S-LOC
, 1996-08-22, O
[HERE SHOULD BE SPACE]
5.381 , BEIJING 1996-08-22, BEIJING , S-LOC
, 1996-08-22, O
CSV file has the format:
int, sentence (concatenated tokens), token_1, tag_1
, token_2, tag_2
, ...
I have tried the following but didn't work for me properly.
import csv
with open('output.csv','w') as f:
for x in [tuple(zip(x[0], x[1], x[2])) for x in data]:
for r in x:
f.write(' '.join(r) + '\n')
f.write('\n')
Traceback: TypeError: 'float' object is not iterable
I also aimed to do as follows:
data = [(value, ' '.join(sent), sent, tag) for value, sent, tag in data]
to start from then I tried the following.
with open('output.csv', 'w') as f:
writer = csv.writer(f , lineterminator='\n')
for value, sent, tokens, tags in data:
writer.writerow(value)
writer.writerow(sent)
for x in [tuple(zip(tokens, tags))]:
for r in x:
writer.writerow(' '.join(r) + '\n')
writer.writerow('\n')
Traceback: Error: iterable expected, not float