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
BashBedlam and atomxkai 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 can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 28,107 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to write variable in a python file then import it in another python file? tatahuft 4 947 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Problems writing a large text file in python Vilius 4 1,009 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 1,050 Nov-21-2024, 11:48 PM
Last Post: haihal
  Pipe traceback to a C program Alexandros 0 704 Oct-22-2024, 12:32 PM
Last Post: Alexandros
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,084 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 958 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,421 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Reading an ASCII text file and parsing data... oradba4u 2 1,439 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 4,359 Apr-24-2024, 05:47 AM
Last Post: Bronjer

Forum Jump:

User Panel Messages

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