Sep-07-2021, 11:25 AM
since you only use pandas to read the file, perhaps a better solution would be:
from pathlib import Path import csv from CreateDict import CreateDict import os class DataPlot: def __init__(self): # Set starting directory same as code os.chdir(os.path.abspath(os.path.dirname(__file__))) self.cd = CreateDict() homepath = Path('.') self.infile = homepath / '01011900-30092016_AllData.txt' def dispatch(self): data = self.read_data() def read_data(self): with self.infile.open() as fp: crdr = csv.DictReader(fp, delimiter=';') for n, row in enumerate(crdr): # Replace print statement with your code for each row print(f"\n{n}: {self.cd.display_dict(row)}") def main(): dp = DataPlot() dp.dispatch() if __name__ == '__main__': main()This will read each row of the csv file into a dictionary format like:
Output:AgencyID: Ambraseys-Finkel
Time: 18.01.1900 15:30:00
Day: 18
Month: 01
Year: 1900
Hour: 15
Minute: 30
Second: 0
MiliSecond: 0
Latitude: 37.7900
Longitude: 28.2100
Depth: -10.0
Fixed: *
Mtype: MS
Magnitude: 4.40
M0:
Rms:
Herr:
Verr:
Gap:
StationNO:
Country: TÜRKÝYE
City: AYDIN
District: Yenipazar
Town:
Other:
EventType: Eq
you will need CreateDict.py:import os class CreateDict: """ Generic Software tools used by Trailmapper. CreateDict.py - Contains methods to simplify node and cell creation within a dictionary Usage: The best way to learn what can be done is to examine the testit function included in this module. new_dict(dictname) - Creates a new dictionary instance with the name contained in dictname add_node(parent, nodename) - Creates a new node (nested dictionary) named in nodename, in parent dictionary. add_cell(nodename, cellname, value) - Creates a leaf node within node named in nodename, with a cell name of cellname, and value of value. display_dict(dictname) - Recursively displays a nested dictionary. Requirements: Trailmapper software: None Python standard library: os Author: Larz60+ -- May 2019. """ def __init__(self): os.chdir(os.path.abspath(os.path.dirname(__file__))) def new_dict(self, dictname): setattr(self, dictname, {}) def add_node(self, parent, nodename): node = parent[nodename] = {} return node def add_cell(self, nodename, cellname, value): cell = nodename[cellname] = value return cell def display_dict(self, dictname, level=0): indent = " " * (4 * level) for key, value in dictname.items(): if isinstance(value, dict): print(f'\n{indent}{key}') level += 1 self.display_dict(value, level) else: print(f'{indent}{key}: {value}') if level > 0: level -= 1 def testit(): # instantiate class cd = CreateDict() # create new dictionary named CityList cd.new_dict('CityList') # add node Boston boston = cd.add_node(cd.CityList, 'Boston') # add sub node Resturants bos_resturants = cd.add_node(boston, 'Resturants') # Add subnode 'Spoke Wine Bar' to parent bos_resturants spoke = cd.add_node(bos_resturants, 'Spoke Wine Bar') cd.add_cell(spoke, 'Addr1', '89 Holland St') cd.add_cell(spoke, 'City', 'Sommerville') cd.add_cell(spoke, 'Addr1', '02144') cd.add_cell(spoke, 'Phone', '617-718-9463') # Add subnode 'Highland Kitchen' to parent bos_resturants highland = cd.add_node(bos_resturants, 'Highland Kitchen') cd.add_cell(highland, 'Addr1', '150 Highland Ave') cd.add_cell(highland, 'City', 'Sommerville') cd.add_cell(highland, 'ZipCode', '02144') cd.add_cell(highland, 'Phone', '617-625-1131') # display dictionary print(f'\nCityList Dictionary') cd.display_dict(cd.CityList) print(f'\nraw data: {cd.CityList}') if __name__ == '__main__': testit()