Python 将信息从文本文档提取到另一个文本文档中
我需要从文本文件中提取某些行。比如说,我正在寻找Python 将信息从文本文档提取到另一个文本文档中,python,file,text,extract,Python,File,Text,Extract,我需要从文本文件中提取某些行。比如说,我正在寻找“abcd1234”。这四个数字每次都不同,但前四个字母保持不变 这就是我到目前为止所做的: infile = file ('//Users//Nhi//Documents//Gene List.rtf', 'r') outfile = file ('//Users//Nhi//Documents//new.docx', 'w') for line in infile: outfile.write("|MmarC5_\d{4}") in
“abcd1234”
。这四个数字每次都不同,但前四个字母保持不变
这就是我到目前为止所做的:
infile = file ('//Users//Nhi//Documents//Gene List.rtf', 'r')
outfile = file ('//Users//Nhi//Documents//new.docx', 'w')
for line in infile:
outfile.write("|MmarC5_\d{4}")
infile.close()
outfile.close()
但是,输出的短语是“|MmarC5\ud{4}”
,而不是每次都不同的4个数字
with open("somefile.txt") as f:
print re.findall("abcd\d{4}",f.read())
这是一种获取任何与“abcd######”匹配的内容的方法。开始阅读教程和文档:要提取内容,您可以使用它,如果您已经演示了自己解决问题的一些方法,那么获得帮助就容易多了……首先,Python不会使用MS Word
.docx
格式,只需使用.txt
。其次,使用re
模块(参见和Joran Beasley的答案)。继续,更新你的问题,人们会帮你找出不可行的细节。