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
#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


Messages In This Thread
RE: Converted Pipe Delimited text file to CSV file - by atomxkai - Feb-05-2022, 12:31 AM

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 1,205 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  very newbie problem on text file zapad 2 320 Apr-12-2024, 06:50 PM
Last Post: zapad
  Tab Delimited Strings? johnywhy 7 709 Jan-13-2024, 10:34 PM
Last Post: sgrey
  file open "file not found error" shanoger 8 1,316 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,639 Nov-09-2023, 10:56 AM
Last Post: mg24
  Replace a text/word in docx file using Python Devan 4 3,857 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 815 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 1,008 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 1,323 Jul-07-2023, 05:44 PM
Last Post: flash77
  Converted EXE file size is too large Rajasekaran 0 1,574 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