Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将信息从文本文档提取到另一个文本文档中_Python_File_Text_Extract - Fatal编程技术网

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的答案)。继续,更新你的问题,人们会帮你找出不可行的细节。