简体   繁体   English

将字符串分成两部分Python

[英]Split the strings into two parts Python

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 所以我现在在第14列中是

AAA/BBB AAA / BBB

and I want to place AAA in column 2 and BBB in column 3. 我想在第2列中放置AAA,在第3列中放置BBB。

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)

我想那就是你想要的

row[1:3] = row[14].split('/')

It's because you are not saving the value after splitting poly. 这是因为拆分多边形后您没有保存该值。 Try the code below: 请尝试以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM