Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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_Regex_File Io - Fatal编程技术网

Python 使用正则表达式读取和处理文件

Python 使用正则表达式读取和处理文件,python,regex,file-io,Python,Regex,File Io,我有一个巨大的文件,它只是这些块的重复单元: //WAYNE ROONEY (wr10) 90 [label="90"]; 90 -> 11 [weight=25]; 90 -> 21 [weight=23]; 90 -> 31 [weight=17]; 90 -> 41 [weight=12]; 90 -> 51 [weight=1]; 90 -> 62 [weight=50]; 90 -> 72 [weight=7];

我有一个巨大的文件,它只是这些块的重复单元:

//WAYNE ROONEY (wr10)
  90 [label="90"];
  90 -> 11 [weight=25];
  90 -> 21 [weight=23];
  90 -> 31 [weight=17];
  90 -> 41 [weight=12];
  90 -> 51 [weight=1];
  90 -> 62 [weight=50];
  90 -> 72 [weight=7];
  90 -> 82 [weight=27];
  90 -> 92 [weight=9];
  90 -> 102 [weight=43];
我需要转换成这样的格式

90 11 25
i、 我只需要去掉所有多余的东西,保持数字不变

我尝试使用regex,代码如下:

for line in filein:
    match = re.search('label=" "', line)
    if match:
        print (match.group())

但它只打印文件中所有
'label'
的实例。如果我尝试搜索
'label=“”
,则没有输出。如果我能知道如何读取标签,那么读取重量将与之非常相似。

要获取每行的所有数字,请使用
r'\d+
.findall()

您不完全清楚如何处理
标签
行,但非常简单的循环会打印出来:

label: 90 90
90 11 25
90 21 23
90 31 17

等。

要获取每行的所有数字,请将
r'\d+
.findall()
一起使用:

您不完全清楚如何处理
标签
行,但非常简单的循环会打印出来:

label: 90 90
90 11 25
90 21 23
90 31 17
等等。

这个怎么样:

import re

file = open("file","r")                       

for line in file:                                 
    if re.search('->',line):
        print ' '.join(re.findall('[0-9]+',line))
输出:

90 11 25
90 21 23
90 31 17
90 41 12
90 51 1
90 62 50
90 72 7
90 82 27
90 92 9
90 102 43
只需重定向以保存输出:
python test.py>newfile

如何:

import re

file = open("file","r")                       

for line in file:                                 
    if re.search('->',line):
        print ' '.join(re.findall('[0-9]+',line))
输出:

90 11 25
90 21 23
90 31 17
90 41 12
90 51 1
90 62 50
90 72 7
90 82 27
90 92 9
90 102 43

只需重定向以保存输出:
python test.py>newfile

您可以使用以下内容匹配所有行:

  • (\d+)
    ->一个数字(反向参考)
  • \s*->\s*
    ->Space->Space
  • (\d+)
    ->另一个数字(反向参考)
  • \s*\[weight=\”
    ->空格和文本[weigth=”
  • (\d+)
    ->另一个数字(反向参考)
  • \];
    ->Literal];结束比赛
  • 然后您有一个编号的反向引用,如下所示:

  • 第一个数字
  • 第二个数字
  • 第三个数字

  • 现在,您可以使用所需的模式构建字符串。($1$2$3)

    您可以使用以下内容匹配所有行:

  • (\d+)
    ->一个数字(反向参考)
  • \s*->\s*
    ->Space->Space
  • (\d+)
    ->另一个数字(反向参考)
  • \s*\[weight=\”
    ->空格和文本[weigth=”
  • (\d+)
    ->另一个数字(反向参考)
  • \];
    ->Literal];结束比赛
  • 然后您有一个编号的反向引用,如下所示:

  • 第一个数字
  • 第二个数字
  • 第三个数字

  • 现在,您可以使用所需的模式构建字符串。($1$2$3)

    标签是节点。节点(90)连接到节点11,且该边的权重为25。我打算通读所有“标签”的文件。@begin.py:很容易检测到字符串
    label
    ,已更新。标签就是节点。节点(90)连接到节点11,且该边的权重为25。我打算通读所有“标签”的文件。@begin.py:检测字符串
    label
    ,很容易,已更新。