删除python

时间:2017-07-13 18:47:46

标签: python

在该列中有一列,可能有也可能没有1到6位数字。数据必须按照原始工作表中列出的顺序保留,例如A1必须保留在第1行,第2行第2行,依此类推。

例如:

Cell A1:
Lipodystrophy: congenital generalized: type 2: 269700; Encephalopathy: progressive: with or without lipodystrophy: 615924; Silver spastic paraplegia syndrome: 270685; and Neuropathy: distal hereditary motor: type VA: 600794

成为:

269700, 615924, 270685, 600794

2 个答案:

答案 0 :(得分:2)

试试这个衬垫;

in_string = ("Lipodystrophy: congenital generalized: type 2: 269700; "
             "Encephalopathy: progressive: with or without lipodystrophy: "
             "615924; Silver spastic paraplegia syndrome: 270685; "
             "and Neuropathy: distal hereditary motor: type VA: 600794")

output = ', '.join([word for word in in_string.replace(';', '').split()
                    if word.isdigit()])

输出;

print(output)
>>> 269700, 615924, 270685, 600794

或者,使用输入文件;

 with open('input.csv') as fin, open('output.csv', 'w') as fout:
    output = '\n'.join(','.join(word for word in line.replace(';', '').split() 
                                if word.isdigit()) for line in fin)
    fout.write(output)

答案 1 :(得分:0)

使用regular expression

import csv 
import re 

with open('input.csv') as fin, 
open('output.csv', 'wb') as fout: 
    csv_in = csv.reader(fin, delimiter = '\t')
    csv_out = csv.writer(fout) 
    for row in csv_in: 
        matchList = re.findall(r'\d{6}', row, flags=0)
        csv_out.writerow(matchList)

模式将类似于" \ d {6}"或" / d / d / d / d / d / d"