Posts: 30
Threads: 8
Joined: Feb 2021
Hello, I'm trying to add some columns with values such as index but I have an error in line 16. Hope anyone can help. Thanks!
import csv
import glob
header = ['NAME','ADDRESS','PHONE','EMAIL']
# with pipe delimter data
with open("datafile.txt", mode='r') as text_pipe:
reader_pipe = csv.reader(text_pipe, delimiter = '|')
with open("output.csv", 'w', newline = '') as file_comma:
writer_delim = csv.writer(file_comma, delimiter = ',')
writer_delim.writerow(header) # add header
record_index = 1 # index sequence
for row in reader_pipe: # loop to read file
writer_delim.writerow(record_index,row) # print value in each line
record_index = record_index + 1 Error: Exception has occurred: TypeError
writerow() takes exactly one argument (2 given)
writer_delim.writerow(record_index,row) # print value in each line
Posts: 1,583
Threads: 3
Joined: Mar 2020
Feb-05-2022, 12:08 AM
(This post was last modified: Feb-05-2022, 12:08 AM by bowlofred.)
writerow() takes exactly one argument: the entire row you want to write/append to your csv.
You've passed in two objects. If you want to add record_index to your CSV, modify row directly to put that value in the row. Then write that row out.
Some other things I might suggest:
You don't need two separate contexts to open two files. If you're just going to exit immediately if either open fails and you're done with them at the same time, then you might want to put them in the same with and save some indent space.
Since you're just counting on a loop, consider using enumerate() for a counter rather than managing your own.
import csv
import glob
header = ['NAME','ADDRESS','PHONE','EMAIL']
# with pipe delimter data
with open("datafile.txt", mode='r') as text_pipe, open("output.csv", 'w', newline= '') as file_comma:
reader_pipe = csv.reader(text_pipe, delimiter = '|')
writer_delim = csv.writer(file_comma, delimiter = ',')
writer_delim.writerow(header) # add header
for record_index, row in enumerate(reader_pipe, 1): # loop to read file
row.insert(0, record_index)
writer_delim.writerow(row) # print value in each line
BashBedlam and atomxkai like this post
Posts: 30
Threads: 8
Joined: Feb 2021
Feb-05-2022, 12:31 AM
(This post was last modified: Feb-05-2022, 12:31 AM by atomxkai.)
(Feb-05-2022, 12:08 AM)bowlofred Wrote: writerow() takes exactly one argument: the entire row you want to write/append to your csv.
You've passed in two objects. If you want to add record_index to your CSV, modify row directly to put that value in the row. Then write that row out.
Some other things I might suggest:
You don't need two separate contexts to open two files. If you're just going to exit immediately if either open fails and you're done with them at the same time, then you might want to put them in the same with and save some indent space.
Since you're just counting on a loop, consider using enumerate() for a counter rather than managing your own.
import csv
import glob
header = ['NAME','ADDRESS','PHONE','EMAIL']
# with pipe delimter data
with open("datafile.txt", mode='r') as text_pipe, open("output.csv", 'w', newline= '') as file_comma:
reader_pipe = csv.reader(text_pipe, delimiter = '|')
writer_delim = csv.writer(file_comma, delimiter = ',')
writer_delim.writerow(header) # add header
for record_index, row in enumerate(reader_pipe, 1): # loop to read file
row.insert(0, record_index)
writer_delim.writerow(row) # print value in each line
It works perfectly bowlofred! Thank you!
What I learn:
1. to write in same 'with'
2. function 'insert'
3. I've tried 'enumerate' before but haven't absorbed it until now.
Extra question.
1. So if i want to insert other values and add new columns or fields, i can just use insert function and use position?
2. Do you have suggestion how to truly understand these types of functions or just practice?
Posts: 1,583
Threads: 3
Joined: Mar 2020
(Feb-05-2022, 12:31 AM)atomxkai Wrote: 1. So if i want to insert other values and add new columns or fields, i can just use insert function and use position?
Yes. You need to feed the csvwriter an object with the correct number of fields. As yours was a list, I used the .insert() method to modify it, but you could use any other manipulation (create a new list with a comprehension, slices, .append() method, etc.)
This is pretty simple, but it is up to you to keep track and make sure everything is always done in the right order.
Quote:2. Do you have suggestion how to truly understand these types of functions or just practice?
Pretty much just practice. Nothing wrong with trying and having a less efficient or less idiomatic at first. I find the reference guide overwhelming when I'm first learning a language. I expect certain basics to be there. It's only after using it for a period that I get comfortable and I can understand how some of the subtleties fit together.
Posts: 30
Threads: 8
Joined: Feb 2021
(Feb-05-2022, 01:10 AM)bowlofred Wrote: (Feb-05-2022, 12:31 AM)atomxkai Wrote: 1. So if i want to insert other values and add new columns or fields, i can just use insert function and use position?
Yes. You need to feed the csvwriter an object with the correct number of fields. As yours was a list, I used the .insert() method to modify it, but you could use any other manipulation (create a new list with a comprehension, slices, .append() method, etc.)
This is pretty simple, but it is up to you to keep track and make sure everything is always done in the right order.
Quote:2. Do you have suggestion how to truly understand these types of functions or just practice?
Pretty much just practice. Nothing wrong with trying and having a less efficient or less idiomatic at first. I find the reference guide overwhelming when I'm first learning a language. I expect certain basics to be there. It's only after using it for a period that I get comfortable and I can understand how some of the subtleties fit together.
Truly awesome, thanks again!
|