Python Forum

Full Version: How to do next line output from CSV column?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
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]