Split the strings into two parts Python

552 views Asked by At

I'm trying to split a '/' connected string in one column and distribute them into two different columns.

So what I have now in column 14 is

AAA/BBB

and I want to place AAA in column 2 and BBB in column 3.

1    2    3    4    5    6

    AAA  BBB

I tried to use string split or strip but nothing really fit my need exactly.

Can anyone please help me?

Thanks in advance.

This is the code that I have so far...

import csv
import re

# reads the input and spits out the output and inserting empty columns for new parameters
with open ('DownloadDBF.csv', 'r') as csvinput:
    with open ('outputCSV.csv', 'w', newline ='') as csvoutput:
        reader = csv.reader(csvinput, delimiter = ',')
        writer = csv.writer(csvoutput,  delimiter = ',')
        all = []
        row = next(reader)
        # inserting empty columns with new headings(paramters)
        row.insert(0,'GenomePosition')
        row.insert(1, 'ReferenceCodon')
        row.insert(2, 'VariantCodon')
        all.append(row)
        poly = []
        for row in reader:
            all.append(row)
            row.insert(0,'') # emptying the column
            row.insert(1,'') # emptying the column
            row.insert(2,'')
            poly = row[14] # polymorphism column saved into 'poly'
            poly.split('/')
            print(poly)
        writer.writerows(all)
2

There are 2 answers

0
Elazar On BEST ANSWER

I guess that's what you wanted

row[1:3] = row[14].split('/')
0
Pankaj Moolrajani On

It's because you are not saving the value after splitting poly. Try the code below:

poly_split = poly.split('/')
print(poly_split)