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:

enter image description here

what doing wrong?

you have 3 ways correct output:

  1. remove commas money amounts.
  2. 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='"')

  3. 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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -