Python Forum
Converted Pipe Delimited text file to CSV file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converted Pipe Delimited text file to CSV file
#1
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
Reply
#2
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
atomxkai and BashBedlam like this post
Reply
#3
(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?
Reply
#4
(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.
atomxkai likes this post
Reply
#5
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 601 1 hour ago
Last Post: Bronjer
  very newbie problem on text file zapad 2 191 Apr-12-2024, 06:50 PM
Last Post: zapad
  Tab Delimited Strings? johnywhy 7 552 Jan-13-2024, 10:34 PM
Last Post: sgrey
  file open "file not found error" shanoger 8 1,082 Dec-14-2023, 08:03 AM
Last Post: shanoger
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,423 Nov-09-2023, 10:56 AM
Last Post: mg24
  Replace a text/word in docx file using Python Devan 4 3,278 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 754 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 919 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 1,201 Jul-07-2023, 05:44 PM
Last Post: flash77
  Converted EXE file size is too large Rajasekaran 0 1,503 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran

Forum Jump:

User Panel Messages

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