Avatar of tmajor99
tmajor99

asked on 

Python - how to parse a string and output as separate columns

I am trying to learn Python and need to help to output a header record that was inputted into the program.  Currently, my program is writing the output to a csv file but i want to output instead.   The program reads in a file and then captures the header record.  I just want to display the header record so each header column is in a separate column.   When i try using "output.SetHeaderCells(data)" , i can see all the header columns as single string.  I need each header column to be in a separate cell so how do split the header row?

Results when I use output.SetHeaderCells(data)
       ID,Description,Part,Cost, Prices......
I need each column to separated not a string.    Expected results;

ID       Description    Part    Cost   Price


My Program:

file1="\\\\\EGO_ITEM_INTF_EFF_B_TEMP.csv"
outputfile="\\\\\EGO_ITEM_INTF_EFF_MR.csv"


with open(file1,'r') as f:
    output1 = f.read()
x=output1.index("\n")
header=output1[:x]
output1=output1[x+1:]
data = header+'\n';
out = output1.split('\n')
output.SetHeaderCells(data)
for row in out:
    
    
    if not row or row.isspace():
        break
    row1 = row.split(',')
    c8 = row1[8].split(';')
    c23 = row1[23].split(';')
    cc8 = []
    for x in c8:            # removing ';', '' , space
        x = x.strip()
        if x and not x.isspace():
            cc8.append(x)   # cleaned 'Component Item Name'
    cc23 = []
    for c in c23:            # removing ';', '' , space
        c = c.strip()
        if c and not c.isspace():
            cc23.append(c)   # cleaned 'Quantity'
    len8 = len(cc8)
    len23 = len(cc23)
    if len8 == 0 and len23 == 0:
        newrow = [row1[0], row1[1], row1[2], row1[3], row1[4], row1[5], row1[6], row1[7], '', row1[9], row1[10], row1[11], row1[12], row1[13], row1[14], row1[15], row1[16], row1[17], row1[18], row1[19], row1[20], row1[21], row1[22], '', row1[24], row1[25], row1[26], row1[27], row1[28]]
        data+= ','.join(newrow)+'\n'
        continue
    
    j=0
    if len8 >= len23:
        for s in cc8:
            p = ''
            if(j<len(cc23)):
                p = cc23[j]
            newrow = [row1[0], row1[1], row1[2], row1[3], row1[4], row1[5], row1[6], row1[7], s, row1[9], row1[10], row1[11], row1[12], row1[13], row1[14], row1[15], row1[16], row1[17], row1[18], row1[19], row1[20], row1[21], row1[22], p, row1[24], row1[25], row1[26], row1[27], row1[28]]
            j+=1
            data+= ",".join(newrow)+'\n'
    else:
        for p in cc23:
            s = ''
            if(j<len(cc8)):
                s = cc8[j]
            newrow = [row1[0], row1[1], row1[2], row1[3], row1[4], row1[5], row1[6], row1[7], s, row1[9], row1[10], row1[11], row1[12], row1[13], row1[14], row1[15], row1[16], row1[17], row1[18], row1[19], row1[20], row1[21], row1[22], p, row1[24], row1[25], row1[26], row1[27], row1[28]]
            j+=1
            data+= ",".join(newrow)+'\n'



#ull1_str = ','.join(col1)
#print(ull1_str)

with open(outputfile, "w") as CSV_file:
        CSV_file.write(data)
        output.AddRow(data)

Open in new window

Python

Avatar of undefined
Last Comment
Louis LIETAER

8/22/2022 - Mon