在Python的re模块中需要帮助
我正在尝试使用re模块解析文件。我累了三个版本的代码,前两个版本没有检索任何O / P.第三个版本只检索一行。有人可以看看吗?
sample.txt中的数据如下。第一行后网络是空白的,当我在python shell上单独运行这些语句时,正则表达式正在运行。
****************************************** ****************************
Oregon Exchange BGP路由查看器
route-views.oregon-ix.net / route-views.routeviews.org
此硬件是NSF资助的一部分。
如果您有任何疑问,请联系help@routeviews.org,或者如果您想提供观点,请联系
.
网络下一跳度量标准。 LocPrf重量路径
* 64.48.0.0/16 173.205.57.234 0 53364 3257 2828 i
* 202.232.0.2 0 2497 2828 i
* 93.104.209.174 0 58901 51167 1299 2828 i
* 193.0.0.56 0 3333 2828 i
* 103.197.104.1 0 134708 3491 2828 i
* 132.198.255.253 0 1351 6939 2828 i
我尝试过:
版本1:
import re
file = open('sample.txt','r')
x = file.readline()
而x:
var = re.findall(r'(?:\ * | \ *>)\s +(\d +。\ d + .\d + .\d + \ / \d + \s +)?(\S +) \\ s + \d + \\\ +(\d + \s +。+)[ie]',x)
x = file.readline()
print(var )
file.close()
版本2:
导入re
file = open('sample.txt','r')
x = file.read()
var = re.findall( R'?(?:\ * | \ * GT)\s +(\d + .\d + .\d + .\d + \ / \d + \s +)(\S +) \ + + \d + \\\ +(\d + \s +。+)[ie]',x)
print(var)
file.close( )
版本3:
导入重新
file = open('sample.txt','r' )
x = file.readline()
而x:
var = re.search(r'(?:\ * | \\ \\ * GT)\s +(\d + .\d + .\d + .\d + \ / \d + \s +)(\S +)\s + \d + \s +(? \ + + \ s +。+)[ie]',x,re.M)
x = file.readline()
print(var.group(0) )
file.close()
I'm trying to use the re module to parse through a file. I tired three version of the code, first 2 version's are not retrieving any O/P. The third version is retrieving only one line. Can someone please have a look?
The data in sample.txt is as below. The network is blank after first line, and when I'm running individually these statements on python shell the regex is working.
**********************************************************************
Oregon Exchange BGP Route Viewer
route-views.oregon-ix.net / route-views.routeviews.org
This hardware is part of a grant by the NSF.
Please contact help@routeviews.org if you have questions, or
if you wish to contribute your view.
Network Next Hop Metric . LocPrf Weight Path
* 64.48.0.0/16 173.205.57.234 0 53364 3257 2828 i
* 202.232.0.2 0 2497 2828 i
* 93.104.209.174 0 58901 51167 1299 2828 i
* 193.0.0.56 0 3333 2828 i
* 103.197.104.1 0 134708 3491 2828 i
* 132.198.255.253 0 1351 6939 2828 i
What I have tried:
Version1:
import re
file = open('sample.txt', 'r')
x = file.readline()
while x:
var = re.findall(r'(?:\*|\*>)\s+(\d+.\d+.\d+.\d+\/\d+\s+)?(\S+)\s+\d+\s+(\d+\s+.+)[ie]',x)
x = file.readline()
print(var)
file.close()
Version2:
import re
file = open('sample.txt', 'r')
x = file.read()
var = re.findall(r'(?:\*|\*>)\s+(\d+.\d+.\d+.\d+\/\d+\s+)?(\S+)\s+\d+\s+(\d+\s+.+)[ie]',x)
print(var)
file.close()
Version3:
import re
file = open('sample.txt', 'r')
x = file.readline()
while x:
var = re.search(r'(?:\*|\*>)\s+(\d+.\d+.\d+.\d+\/\d+\s+)?(\S+)\s+\d+\s+(\d+\s+.+)[ie]',x, re.M)
x = file.readline()
print(var.group(0))
file.close()
我刚刚运行了你的代码,除了列出空白条目外,它还可以正常工作:
I just ran your code and it works fine, apart from listing the blank entries:
[]
[]
[]
[]
[]
[]
[]
[]
[('64.48.0.0/16 ', '173.205.57.234', '53364 3257 2828 ')]
[]
[('', '202.232.0.2', '2497 2828 ')]
[]
[('', '93.104.209.174', '58901 51167 1299 2828 ')]
[]
[('', '193.0.0.56', '3333 2828 ')]
[]
[('', '103.197.104.1', '134708 3491 2828 ')]
[]
[('', '132.198.255.253', '1351 6939 2828 ')]
如果你把它放您在< pre>内的代码标签因此:
It would help if you put your code within <pre> tags thus:
import re
file = open('sample.txt', 'r')
x = file.readline()
while x:
var = re.findall(r'(?:\*|\*>)\s+(\d+.\d+.\d+.\d+\/\d+\s+)?(\S+)\s+\d+\s+(\d+\s+.+)[ie]',x)
x = file.readline()
print(var)
file.close()
所以我们可以看看你的缩进是否正确。
so we can see whether your indentations are correct.