【问题标题】:Extracting information from text document into another text document将文本文档中的信息提取到另一个文本文档中
【发布时间】:2013-08-22 17:54:38
【问题描述】:

我需要从文本文件中提取某些行。假设我正在寻找"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_\d{4}",而不是每次都不同的4个数字。

【问题讨论】:

  • 开始阅读教程和文档:for files 并提取您可以使用的内容re
  • 如果您已经证明自己付出了一些努力来解决问题,那么获得帮助会更容易......
  • 首先,Python 不会以 MS Word .docx 格式编写...,只需使用 .txt。其次,使用re 模块(参见docs 和Joran Beasley 的回答)。继续前进,更新您的问题,人们会帮助您找出不起作用的细节。

标签: python file text extract


【解决方案1】:
 with open("somefile.txt") as f:
      print re.findall("abcd\d{4}",f.read())

是获取与“abcd####”匹配的任何内容的一种方式

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 2013-03-09
    • 2015-05-10
    • 2017-10-22
    相关资源
    最近更新 更多