sed 's/\t/_tab_/3g'
我有一个 sed 命令,它基本上替换了我的文本文档中所有多余的制表符分隔符。 我的文档应该是 3 列,但偶尔会有一个额外的分隔符。我无法控制这些文件。
我使用上面的命令来清理文档。然而,我对这些文件的所有其他操作都是在 python 中进行的。有没有办法在 python 中执行上述 sed 命令?
示例输入:
Column1 Column2 Column3
James 1,203.33 comment1
Mike -3,434.09 testing testing 123
Sarah 1,343,342.23 there here
示例输出:
Column1 Column2 Column3
James 1,203.33 comment1
Mike -3,434.09 testing_tab_testing_tab_123
Sarah 1,343,342.23 there_tab_here
最佳答案
你可以逐行读取文件,用tab分割,如果超过3条,用_tab_
加入第3条之后的条:
lines = []
with open('inputfile.txt', 'r') as fr:
for line in fr:
split = line.split('\t')
if len(split) > 3:
tmp = split[:2] # Slice the first two items
tmp.append("_tab_".join(split[2:])) # Append the rest joined with _tab_
lines.append("\t".join(tmp)) # Use the updated line
else:
lines.append(line) # Else, put the line as is
参见 Python demo
lines
变量将包含如下内容
Mike -3,434.09 testing_tab_testing_tab_123
Mike -3,434.09 testing_tab_256
No operation here
关于python - sed to python替换a中的额外分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51317050/