Python - remove brackets and commas from a CSV Tuple file

I print the csv file as follows:

bdictionary = ## bdictionary is a big list of tuples
w = csv.writer(open("testnucsv.csv", "w"))
for sent in bdictionary:
    w.writerow(sent)

And it prints perfectly fine and looks like this:

(u'My ', u'D') (u'dog ', u'N') ............... (u'The ', u'D') .. ..........................

How can i print it like

My dog ​​is DD

This is what I tried and it does not work. It breaks each character:

w = csv.writer(open("testnucsv.csv", "w"))
for sent in bdictionary:
    sent = ''.join(str(v) for v in sent)
    w.writerow(sent)
+4
source share
1 answer

Wrap it in a list, writerow expects iterability, so it iterates over your string, dividing it into separate characters:

sent = [' '.join(" ".join(v) for v in sent)]

You also need to join the lines in the tuple as described above, do not call str on the tuple ie:

t = [(u'My', u'D'), (u'dog', u'N')]

print(" ".join([" ".join(v) for v in t]))
My D dog N

You can also just use file.write and pass the concatenated string to it:

with open("testnucsv.csv", "w") as f:
   for sent in bdictionary:
        f.write(" ".join([" ".join(v) for v in sent])+"\n")
+2
source

All Articles