Python Forum
How to do next line output from CSV column?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do next line output from CSV column?
#1
Hello, I have 4 columns in CSV and I want to make the output for COL4 as next line.
CSV Columns
COL1 COL2 COL3 COL4

output:
<line1> Col2
<line2> Col3
<line3> Col4

thank you! Smile

#def pctUser():
    jobnum = str(input("Enter Work Number: "))
    jobnam1 = str(input("Type Project: "))
    jobnam2 = str(input("Type Description: "))

    # read file names into a list
    en_dir = glob.glob('Job *.txt')
    
    # prep for writing data file
    with open('DF_' + jobnum + '.txt', 'w') as d:
    
    #   write data header line
        d.write('REC\tCOL1\tCOL2\tCOL3\n')
    
        rec = 1
    #   open data file
        with open(en_dir[0]) as f:
            fline = ' '
                
    #       read data
            while fline != '':
                fline = f.readline()
                if fline == '':
                    break
    
    #           write <record number>, <work number>-<record number> and <data>
                d.write(str(rec) + '\t' + str(jobnum) + '-' + str(rec) + '\t')
    
    #           write data line
                d.write(fline)
    
    #           increment record counter
                rec = rec + 1
    
    nTotal = rec - 1
    
    #prep for writing output file
    with open('DV_' + jobnum + '.txt', 'w') as v:
    
    #   write dv header
        v.write('Job No. ' + jobnum + '   (prepared by me)\n')
        v.write(jobnam1 + '\n')
        if jobnam2 != '':
            v.write(jobnam2 + '\n')
        v.write('\nTotal - ' + str(nTotal) + '\n\n')
        v.write('Print file - D_' + jobnum + '.txt\n\n')
        v.write('First 9 records + last record:\n\n')
    
        rec = 1
    #   open data file again
        with open(en_dir[0]) as f:
            fline = ' '
    
    #       write 9 lines of output
            while fline != '':
                fline = f.readline()
                if fline == '':
                    break
    
    #           write output lines
                if rec < 10:
                    v.write(jobnum + '-' + str(rec) + '\n')
                    v.write(fline + '\n')
                    #changes
                    v.write()
    
                if rec == nTotal:
                    v.write(jobnum + '-' + str(rec) + '\n')
                    v.write(fline)
    
    #           increment record counter
                rec = rec + 1
Reply
#2
there are several methods available for writing csv files.
first import csv
then see documentation and example code here: https://docs.python.org/3/library/csv.html#module-csv
atomxkai likes this post
Reply
#3
Not sure I understand the request, but this will get you column 4 as a comma separated string, if that is what you want.
You could write string to a csv file.
You can probably do this more elegantly using pandas.

# read straight to a dictionary
# the columns are: fname, lname, department, birthday_month
# so column 4 is birthday_month
import csv
path = '/home/pedro/myPython/csv/'
with open(path + 'employees.txt', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    name_records = list(csv_reader) # gives a list of dictionaries where the key is the column header

# here column 4 is 'birthday_month'
# get all   'birthday_month' records as a comma-separated string   
string = ''
for d in name_records:
    string = string + d['birthday_month'] + ','
    # get rid of the last comma
    string = string[0:-1]
atomxkai likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading and storing a line of output from pexpect child eagerissac 1 4,149 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  [Solved] Reading every nth line into a column from txt file Laplace12 7 5,144 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  beginner text formatting single line to column jafrost 4 3,162 Apr-28-2021, 07:03 PM
Last Post: jafrost
  How to input & output parameters from command line argument shantanu97 1 2,508 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Add a new line to a CSV in one column bazcurtis 7 2,941 Sep-01-2020, 01:31 PM
Last Post: bazcurtis
  Jupiter Notebook does not show output line Renym 3 2,610 Apr-26-2020, 11:21 AM
Last Post: jefsummers
  [gpxpy] "Error parsing XML: not well-formed (invalid token): line 1, column 1" Winfried 5 6,591 Jan-26-2020, 01:09 AM
Last Post: Winfried
  A question about subprocess taking input from command line and returning output! Aurimas 8 5,109 May-15-2019, 04:02 PM
Last Post: Aurimas
  Writing values at a desired column in a line of text file Gupta 3 3,437 Jul-28-2017, 11:08 PM
Last Post: Larz60+
  String output displaying \n instead of new line... nicklesprout 4 25,748 Jul-28-2017, 08:01 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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