Quote:What does this part of the code below? Could you provide some explanation behind the lines of code as comments?
for example, filename = Path('/media/stubens/DataDrive-3XT/projs/T-Z/T/TryStuff/data/csv/IllinoisData.csv')
def read_csv_file(filename):
parts = list(filename.parts)
# parts = ['/', 'media', '/', 'stubens', '/', 'DataDrive-3XT','/', 'projs', '/', 'T-Z', '/', 'T', '/', 'TryStuff', '/', 'data', '/', 'csv', '/', 'IllinoisData.csv']
parts[-1] = f"{filename.stem}Modified{filename.suffix}"
# parts[-1] = 'IllinoisDataModified.csv'
# parts = ['/', 'media', '/', 'stubens', '/', 'DataDrive-3XT','/', 'projs', '/', 'T-Z', '/', 'T', '/', 'TryStuff', '/', 'data', '/', 'csv', '/', 'IllinoisDataModified.csv']
parts[0] = ''
# removes '/' from index 0 because join will add it back again
# parts = ['media', '/', 'stubens', '/', 'DataDrive-3XT','/', 'projs', '/', 'T-Z', '/', 'T', '/', 'TryStuff', '/', 'data', '/', 'csv', '/', 'IllinoisDataModified.csv']
outfilename = Path(f"{'/'.join(parts)}")
# join fileparts back together
# outfilename = '/media/stubens/DataDrive-3XT/projs/T-Z/T/TryStuff/data/csv/IllinoisDataModified.csv'
print(f"new output file name: {outfilename}")
# will print
# '/media/stubens/DataDrive-3XT/projs/T-Z/T/TryStuff/data/csv/IllinoisDataModified.csv'
# open two files, filename as input (fp), outfilename as output (fout)
with filename.open() as fp, outfilename.open('w') as fout:
# set csv reader to flename (fp)
crdr = csv.reader(fp, delimiter=',')
# set csv writer to outfilename (fout), delimiter = ','
cwrtr = csv.writer(fout, delimiter=',')
# for each row in crdr (csv input)
for row in crdr:
# print the row
print(row)
# ...
# Your code goes here
# ...
# write modified row
cwrtr.writerow(row)