I am writing the contents of a dictionary to a csv file. The k,v pairs of my dictionary are {int: list of ints}. I am seeing a funny thing when it is written to the file => when the size of the list is one, it prints as a list. When the size of the list is greater, it prints as a string (in double-quotes). Here is an example output (as seen in the output csv file):
24,[-12345]
25,[-1962956450172838896]
26,"[-125, -12323459939, -2323445, -345425]"
27,[-730881498578617677]
20,[4001623946089138114]
21,[-12345]
I use the following code to write to a file:
def dictToCsv(dictionary, csvfile):
writer = csv.writer(open(csvfile, 'wb'))
for key, value in dictionary.items():
writer.writerow([key, value])
I am positive that all values are list of integers.
I create the dictionary this way (for brevity, I have shortened the code):
memberList = []
d = {}
with open(inputCsvFile) as f_in:
for line in f_in:
pair = line.split(',')
cid = pair[0]
member = pair[1]
value = member.strip()
memberList.append(int(value))
d[cid] = memberList
What could be going wrong? I tried to print the dict values at all stages...and they are just lists. Not sure how they are being transformed to strings. I just do not want the double-quotes in my output csvfile.
Any help is appreciated.
["25", "[-1962956450172838896]"]as the value for row 2 and["26", "[-125, -12323459939, -2323445, -345425]"]for row 3. The only type in a csv fle is string.