在python中从文本文件中删除单词而不是数字

在python中从文本文件中删除单词而不是数字,python,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;

有一列,在该列中,可能有或可能没有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
使用

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

试试这一行

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)

您的输入格式是什么?导入csv导入re,以open('input.csv')作为fin,以open('output.csv','wb')作为fout:csv_in=csv.reader(fin,delimiter='\t')csv_out=csv.writer(fout)作为csv中的行,以:result=re.sub('[^0-9],'',row)csv_out.writerow(row)作为行)你能详细说明“将其放入”的含义吗?
csv\u in.替换
将不起作用,因为
csv\u in
不是字符串。我将编辑我的答案,以便在几秒钟内将其包括在内。你不需要使用csv模块。您也不应该将数据输入指定为
'wb'
,因为您没有写入字节。我现在已经更新了我的答案,以适合您使用csv的用例files@a.mah现在编辑我的答案以作为单个列输出。
 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)