Python 如何以str格式返回(而不是打印)所有匹配行?字符串是一个长字符串,由\t';标准普尔\n';s 这就是我一直在寻找并最终得到的代码。谢谢你的提示。。。
假设您的其余代码(包括正则表达式)是正确的Python 如何以str格式返回(而不是打印)所有匹配行?字符串是一个长字符串,由\t';标准普尔\n';s 这就是我一直在寻找并最终得到的代码。谢谢你的提示。。。,python,regex,Python,Regex,假设您的其余代码(包括正则表达式)是正确的 def Parser(string): string = string.split('\n') firstline = string.pop(0) import re matches = '' for line in string: line = re.search(r"\S+\t+(\S+\t+)\S+\t+\S+\t+(\S+)\t+\S+", line) if line: match = line.g
def Parser(string):
string = string.split('\n')
firstline = string.pop(0)
import re
matches = ''
for line in string:
line = re.search(r"\S+\t+(\S+\t+)\S+\t+\S+\t+(\S+)\t+\S+", line)
if line:
match = line.group(1) + line.group(2)+'\n'
matches += match
return matches
考虑对输入使用解析器。Python附带了
csv
模块:
def Parser(string):
string = string.split('\n')
import re
matches = []
for line in string:
line = re.search(r"\S+\t+(\S+\t+)\S+\t+\S+\t+(\S+)\t+\S+", line)
match = line.group(1) + line.group(2)
matches.extend(match)
return matches
要匹配的字符串示例?谢谢您的提示。我需要str格式的输出,但我最终得到了。
def Parser(string):
string = string.split('\n')
import re
matches = []
for line in string:
line = re.search(r"\S+\t+(\S+\t+)\S+\t+\S+\t+(\S+)\t+\S+", line)
match = line.group(1) + line.group(2)
matches.extend(match)
return matches
import csv
def Parser(string):
output = []
for fields in csv.reader(string.split('\n'), 'excel-tab'):
if len(fields) >= 6:
output.append( fields[1] + '\t' + fields[4] )
return output