Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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 如何从txt文件中获取带有regex的ID?_Python_Regex - Fatal编程技术网

Python 如何从txt文件中获取带有regex的ID?

Python 如何从txt文件中获取带有regex的ID?,python,regex,Python,Regex,我想从如下所示的文本文件中获取带有regex的ID: Id: 1 ASIN: 0827229534 title: Patterns of Preaching: A Sermon Sampler group: Book salesrank: 396585 similar: 5 0804215715 156101074X 0687023955 0687074231 082721619X categories: 2 |Books[283155]|Subjects

我想从如下所示的文本文件中获取带有regex的ID:

Id:   1
ASIN: 0827229534
  title: Patterns of Preaching: A Sermon Sampler
  group: Book
  salesrank: 396585
  similar: 5  0804215715  156101074X  0687023955  0687074231  082721619X
  categories: 2
   |Books[283155]|Subjects[1000]|Religion & Spirituality[22]|Christianity[12290]|Clergy[12360]|Preaching[12368]
   |Books[283155]|Subjects[1000]|Religion & Spirituality[22]|Christianity[12290]|Clergy[12360]|Sermons[12370]
  reviews: total: 2  downloaded: 2  avg rating: 5
    2000-7-28  cutomer: A2JW67OY8U6HHK  rating: 5  votes:  10  helpful:   9
    2003-12-14  cutomer: A2VE83MZF98ITY  rating: 5  votes:   6  helpful:   5  
到目前为止,这是我的代码,但它返回一个空列表,有人能帮我吗

import pandas as pd
import re
regex=r'^Id:(\s*\d*)'
textfile = open("amazon-meta.txt", 'r')
filetext = textfile.read()
matches = re.findall(regex, filetext)
matches

尝试使用
flags=re.MULTILINE

Ex:

import re
with open(filename, "r") as infile:
    print( re.findall(r'^Id:\s*(\d*)', infile.read(), flags=re.MULTILINE))

请尝试^Id:(.*+)\n根据问题的文本,您的代码正确地为我返回
['1']
。您确定
amazon meta.txt
的内容正确吗?你展示的只是文件的一部分吗?