Splitting a delimited file and storing into new column

439 Views Asked by At

I am trying to split csv file. After reading the delimited file, I want to split desired column furthur. My sample code:

import csv
sample = open('~/sample.txt')
adr = csv.reader(sample, delimiter='|')
for row in adr:
    a = row[0]
    b = row[1]
    c = row[2]
    d = row [3]
    new=""
    new = row[4].split(",")
    for row1 in new:
        print row1

sample.txt file contains:

aa|bb|cc|dd|1,2,3,4|xx
ab|ax|am|ef|1,5,6|jk
cx|kd|rd|j|1,9|k

Above code produce output as:

[1,2,3,4]
[1,5,6]
[1,9]

I am trying to further split new column and going to use splited output for comparison. For example, desired output for splitting will be :

aa|bb|cc|dd|1|2|3|4|xx
ab|ax|am|ef|1|5|6| |jk
cx|kd|rd|j|1|9| | |k

Also I want to store mutiple blank or NULL value of new column, as shown in above example [1,2,3,4], [1,5,6]. Is there better way to split?

1

There are 1 best solutions below

4
On BEST ANSWER

You're pretty much there already! A few more lines after new = row[4].split(",") are all you need.

for i in range(len(new), 4):
    new.append('')
newrow = row[0:4] + new + row[5:]
print('|'.join(newrow))

Edit 2: addressing your comments below in the simplest way possible, just loop through it twice, looking for the longest "subarray" the first time. Re: printing extra times, you likely copied the code into the wrong place/indentation and have it in the loop.

Full code:

import csv
sample = open('~/sample.txt')
adr = csv.reader(sample, delimiter='|')
longest = 0
for row in adr:
    curLen = len(row[4].split(','))
    if curLen > longest:
        longest = curLen

sample.seek(0)
for row in adr:
    new = row[4].split(",")
    for i in range(len(new), longest):
        new.append(' ')
    newrow = row[0:4] + new + row[5:]
    print('|'.join(newrow))