1

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.

3
  • 3
    They're all being sent to the csv file as string representations of lists. When there is more than one item in a list it needs to be quoted in the csv file so the commas that separate the items in the list are not mistaken for the commas that separate the fields in the csv file. If you reread that csv file back into python using the csv module. You will get back ["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. Commented Jan 13, 2016 at 19:26
  • 1
    There are different flavors of csv. As Steven noted, the comma-containing list representations need to be quoted because of ambiguity of the comma. You could change to a csv flavor that uses semicolon as an item separator, or use quotes even if not necessary, to get similar output for single-itemed and multi-itemed lists. For details, see the python documentation of the csv module, and especially the Dialect section: (docs.python.org/3/library/csv.html#csv.Dialect) Commented Jan 13, 2016 at 19:31
  • without the quotes, your csv file would not work, and would not be readable... Commented Jan 13, 2016 at 19:38

2 Answers 2

2

The lists being sent to the csv file as string representations of lists. When there is more than one item in a list it needs to be quoted in the csv file so the commas that delimit the items in the list are not mistaken for the commas that delimit the fields in the csv file.

The only datatype in a csv file is string. If you reread that csv file back into python using the csv module, you will get back ["25", "[-1962956450172838896]"] as the value for row 2 and ["26", "[-125, -12323459939, -2323445, -345425]"] for row 3.

If you would like to avoid having your lists quoted you could use a different delimiter by setting the delimiter argument when creating your csv file. Tab delimited (\t) might work nicely here.

Sign up to request clarification or add additional context in comments.

2 Comments

This should be bolded, highlighted, and underlined: The only type in a csv file is string.
Many thanks for the explanation. I have started using tab-delimiter.
1

the dictToCsv need to be fixed this way

def dictToCsv(dictionary, csvfile):
    writer = csv.writer(open(csvfile, 'wb'))
    for key, value in dictionary.items():
       # writer.writerow([key, value]) << wrong
       writer.writerow([key] +  value)

this will produce a csv file that looks like:

20,4001623946089138114
21,-12345
24,-12345
25,-1962956450172838896
26,-125,-12323459939,-2323445,-345425
27,-730881498578617677

First element of each line is the key, subsequent elements are the associated list values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.