0

I wrote a program to print the column names of csv file.

The following code prints a dictionary, while I want column names as list.

import csv

def read_csv_fieldnames(filename, separator, quote):    
    with open(filename) as csv_file:
        table = []
        file_reader = csv.DictReader(csv_file, delimiter=separator, quotechar=quote)
        for row in file_reader:
            table.append(row)
            break
    return table

table = read_csv_fieldnames("hightemp.csv", ',',"'")    
print(table[0])

Output generated:

{'City': 'Houston', 'Jan': '62', 'Feb': '65', 'Mar': '72', 'Apr': '78', 'May': '84', 'Jun': '90', 'Jul': '92', 'Aug': '93', 'Sep': '88', 'Oct': '81', 'Nov': '71', 'Dec': '63'}

How can I get this required output?

['City', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

mkrieger1
  • 14,486
  • 4
  • 43
  • 54

0 Answers0