python - CSV module issue with csv files containing extra commas -
i following along book foundation analytics python clinton w. brownley (o'reilly media inc.)
for chapter 2 - read , write csv file (part 2) base python, csv module
the script following:
#!/usr/bin/env python3 import sys import csv input_file = sys.argv[1] output_file = sys.argv[2] open(input_file, 'r', newline='') csv_input_file: open(output_file, 'w', newline='') csv_output_file: filereader = csv.reader(csv_input_file, delimiter=',') filewriter = csv.writer(csv_output_file, delimiter=',') row_list in filereader: print(row_list) filewriter.writerow(row_list)
the input file has fields containing commas (the dollar amounts in last 2 lines):
supplier name,invoice number,part number,cost,purchase date supplier x,001-1001,2341,$500.00,1/20/14 supplier x,001-1001,2341,$500.00,1/20/14 supplier x,001-1001,5467,$750.00,1/20/14 supplier x,001-1001,5467,$750.00,1/20/14 supplier y,50-9501,7009,$250.00,1/30/14 supplier y,50-9501,7009,$250.00,1/30/14 supplier y,50-9505,6650,$125.00,2/3/14 supplier y,50-9505,6650,$125.00,2/3/14 supplier z,920-4803,3321,$615.00,2/3/14 supplier z,920-4804,3321,$615.00,2/10/14 supplier z,920-4805,3321,$6,015.00,2/17/14 supplier z,920-4806,3321,$1,006,015.00,2/24/14
running script produces following output in terminal:
['supplier name', 'invoice number', 'part number', 'cost', 'purchase date'] ['supplier x', '001-1001', '2341', '$500.00', '1/20/14'] ['supplier x', '001-1001', '2341', '$500.00', '1/20/14'] ['supplier x', '001-1001', '5467', '$750.00', '1/20/14'] ['supplier x', '001-1001', '5467', '$750.00', '1/20/14'] ['supplier y', '50-9501', '7009', '$250.00', '1/30/14'] ['supplier y', '50-9501', '7009', '$250.00', '1/30/14'] ['supplier y', '50-9505', '6650', '$125.00', '2/3/14'] ['supplier y', '50-9505', '6650', '$125.00', '2/3/14'] ['supplier z', '920-4803', '3321', '$615.00', '2/3/14'] ['supplier z', '920-4805', '3321', '$615.00', '2/17/14'] ['supplier z', '920-4804', '3321', '$6', '015.00', '2/10/14'] ['supplier z', '920-4806', '3321', '$1', '006', '015.00', '2/24/14']
but book show expected output this:
what doing wrong?
you have 3 ways correct output:
- remove commas money amounts.
use quoting: wrap money amount in double quotes. example, in first row $500.00 "$500.00". quoting popular technique. when using quoting, change read statement this:
filereader = csv.reader(csv_input_file, delimiter=',', quotechar='"')
use different delimiter. don't have use comma delimiter. use method, change delimiters in input file delimiter. pipe-delimited files because pipes used text.
filereader = csv.reader(csv_input_file, delimiter='|')
Comments
Post a Comment