[英]Python - Extract specific lines from tab delimited file based on a value in a column
[英]Regex + Python to remove specific trailing and ending characters from value in tab delimited file
自完成任何正则表达式以来已经有好几年了,所以在这里寻求专家的帮助,因为这可能是一件微不足道的练习:)
我有一个制表符分隔的文件,并且在每一行上都有某些字段,其值如下:
(文件中的完整行可能类似于:
123 \\ t b'bar foo' \\ tabc \\ t123 \\ r \\ n
我想摆脱每一行中该字段的所有前导b',b”和尾随“,'。 因此,鉴于上面的示例行,在运行正则表达式后,我将得到:
123 \\ t bar foo \\ tabc \\ t123 \\ r \\ n
如果您可以给我python blurb以便在文件上运行它,将获得加分。
(^ | \\ t)b [\\“']应该与前导符匹配,对于尾随符:
\\“'应该做
在Python中,您可以执行以下操作:
import re
r1 = re.compile("(^|\t)b[\"']")
r2 = re.compile("[\"'](\t|$)")
然后就用
r1.sub("\\1", yourString)
r2.sub("\\1", yourString)
您可以使用的每一行
re.sub(r'''(?<![^\t\n])\W*b(["'])(.*)\1\W*(?![^\t\n])''', r'\2', line)
对于奖励积分:
import re
pattern = re.compile(r'''(?<![^\t\n])\W*b(["'])(.*?)\1\W*?(?![^\t\n])''')
with open('outfile', 'w') as outfile:
for line in open('infile'):
outfile.write(pattern.sub(r'\2', line))
>>> "b\"foo's bar\"".replace('b"',"").replace("b'","").rstrip("\"'")
"foo's bar"
>>> "b'bar foo'".replace('b"',"").replace("b'","").rstrip("\"'")
'bar foo'
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.