Parsing a csv file in python and saving to a dictionary which is in turn saved to a list -


my task read input datafile, line line, , first 10 lines (not including header) split each line on "," , each line, create dictionary key header title of field, , value value of field in row.

the function parse_file() should return list of dictionaries, each data line in file being single list entry.

field names , values should not contain whitespace, spaces or newline characters.

my question program generates data(list) has same value in list entries last line of csv file.

 import os   datadir = ""  datafile = "beatles-diskography.csv"    def parse_file(datafile):     data = []     count = 0     d = 0     my_dict = dict()     open(datafile, "r") f:        while d<10:          line in f:             count = count + 1             if count 1:              p = line.split(',')              length = len(p)             else:              r = line.split(',')               l = 0              while l < length:                  my_dict[p[l].strip('\n')] = r[l].strip('\n')                   l = l + 1                     data.append(my_dict)                    d = d + 1            return data   def test():                      # simple test of implementation      datafile = os.path.join(datadir, datafile)       d = parse_file(datafile)       firstline = { 'title':              'please please me',                    'uk chart position':  '1',                    'label':              'parlophone(uk)',                    'released':           '22 march 1963',                    'us chart position':  '-',                    'riaa certification': 'platinum',                    'bpi certification':  'gold'                    }       tenthline = { 'title':              '',                    'uk chart position':  '1',                    'label':              'parlophone(uk)',                    'released':           '10 july 1964',                    'us chart position':  '-',                    'riaa certification': '',                    'bpi certification':  'gold'                    }       assert d[0] == firstline      assert d[9] == tenthline   test() 

this example solution using 'csv' library.

import csv  def parse_file(datafile, lines):     open(datafile, 'r') fd:         dat    = csv.reader(fd)         header = next(dat) # makes strong assumption csv has header         retval = list()          index, row in enumerate(dat):             if (index >= lines): break # restricts number of lines             retval.append(dict(zip(header, row)))      return retval  d = parse_file(datafile, 10) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -