0

Im trying to read a CSV file (with one column) and convert them into Lists. The following is my code.

import csv

path = "/tmp/list.csv"
with open(path, 'r') as f:
  reader = csv.reader(f)
  your_list = list(reader)

print(your_list)

List.csv:

apple
banana
orange
pear

The code output:

[['apple'], ['banana'], ['orange'], ['pear']]

But do can I generate a output is the following format?

Expected output:

["apple","banana","orange","pear"]
Karthik
  • 363
  • 2
  • 7
  • 20
  • try printing a few more things, for example reader, what else can you do with reader, https://docs.python.org/3.1/library/csv.html – Christophe Roussy Jul 24 '17 at 09:26
  • import itertools your_list = [['apple'], ['banana'], ['orange'], ['pear']] result = list(itertools.chain(*your_list)) – Asad Iqbal Jul 24 '17 at 10:13
  • I managed to get this closer using "reduce(operator.concat, your_list)", also itertools work the same way. But the output is with single quotes, Can we modify in double quotes? – Karthik Jul 24 '17 at 10:45

0 Answers0