Python Forum
Adding Columns to CSV using iterator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding Columns to CSV using iterator
#9
Sorry, here is the code, in tags. It seems the for loop will run once for row_ID and once for row hence the double entry in my CSV file. Maybe I'm wrong about why this is happening but it is happening. I have tried variations to fix it but to no avail. You will note that the two numbers do start at different points (as below, one starts at 60000 and the other at 700000). Thank you again for your help!

with open(in_file, 'r') as in_f, open(out_file, 'w', newline='') as out_f:
    rdr =  csv.DictReader(in_f)
    fieldnames =  ['Purchase Order Number', 'Purchases Transaction ID Number']
    fieldnames.extend(rdr.fieldnames)
    wrtr = csv.DictWriter(out_f, fieldnames=fieldnames)
    wrtr.writeheader()
    for row_id, row in enumerate(rdr, start=1):
        row['Purchase Order Number'] = '{:0>7}'.format(row_id + 60000)
        row['Purchases Transaction ID Number'] = '{:0>6}'.format(row_id + 700000)
        wrtr.writerow(row)
Reply


Messages In This Thread
Adding Columns to CSV using iterator - by pstarrett - Jan-09-2018, 05:00 AM
RE: Adding Columns to CSV using iterator - by buran - Jan-09-2018, 08:19 AM
RE: Adding Columns to CSV using iterator - by buran - Jan-16-2018, 07:10 AM
RE: Adding Columns to CSV using iterator - by buran - Jan-19-2018, 07:17 AM
RE: Adding Columns to CSV using iterator - by pstarrett - Jan-21-2018, 05:32 AM
RE: Adding Columns to CSV using iterator - by buran - Jan-21-2018, 07:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Merging rows and adding columns based on matching index pythonnewbie78 3 825 Dec-24-2023, 11:51 AM
Last Post: Pedroski55
  numpy adding columns rwahdan 4 2,344 Sep-21-2021, 08:25 PM
Last Post: deanhystad
  Tesseract-ocr ->iterator.WordFontAttributes() does not work Maia07 0 3,425 Sep-01-2018, 02:43 PM
Last Post: Maia07

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020