I have a dictionary like this:
for i in wordlist:
#some searching and parsing that produces one-line sentences, str1 and str2
list1.append(str1)
list2.append(str2)
zipall = zip(list1, list2)
mydict = {i: zipall}
where 'i' is a string. Everything is Cyrillic. When I print it, I get code points (\u0440\u0435 etc.).
I need to save the dictionary to a csv file row by row in every iteration so that i, str1 and str2 are in the same row and in separate columns, to be read by a user later on. When I try
with open('C:\...result.csv','wb') as f: #I will need the 'a' mode?
writer = csv.writer(f)
for key, value in mydict.items():
writer.writerow([key, value])
and similar methods, I get this:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
Other stuff I've tried:
f = open('C:\...results.csv','wb')
w = csv.DictWriter(f,sorted(mydict.keys()))
w.writeheader() #throws error in this line
w.writerow({k:v.encode('utf8') for k,v in mydict.items()})
f.close()
(from this question), and pickle, based on this question. I've been trying to iterate over the dictionary, but the value is a tuple and I can't encode it. There are answers which involve functions and what not (tried working with tuples), but I don't understand those methods (and they didn't work).
Is there a (simple) way?
EDIT - ROUNDABOUT SOLUTION
Since I don't really need the output to be csv, and the data will later be examined in Excel, I applied the xlwt package. Got the idea from here.
The package enables me to write into cells of an Excel spreadsheet with the specified encoding (see this). I don't need dictionaries nor lists of tuples anymore. I just work with the result strings.
If there is a way to convert the xls to csv from Python, I'm not aware of it.