尽管我看到了许多类似的问题,但是我的分隔符不是诸如“ \”或“ *”之类的特殊字符,因此,所有解决方案均无效。我正在将结果写入python中的文件,然后重新打开以进行读取和处理。
file1.txt
control1
1 10 12
1 34 44
2 1 -3
control2
3 4 -10.3
3 3.390 4
我将每个条目分开,直到我看到chapters
中有'控制'的行:
import re
import sys, string, glob, os
with open('file1.txt') as f:
with open("control_output.txt", "w") as output:
mytext = f.read()
chapter = re.split("control[0-2]+\n", mytext)
i=1
print chapter[i]
output.write(chapter[i])
for filename in glob.glob(os.path.join(filePath, 'control_output.txt')):
merged_table=open(filename,'r')
for line in merged_table:
line = line.strip().split('\t')
print line
但是它什么也不打印,因为该行没有制表符分隔符。如果我在读取文件之前退出了脚本,并将所有空格都更改为制表符,那么它将起作用:
sed -i 's/ \+ /\t/g' control_output.txt
然后我有输出:
['1', '10', '12']
['1', '34', '44']
['2', '1', '-3']
我也尝试过subprocess.call但是>
subprocess.call(["sed", "-i", 's/ \+ /\t/g', "control_output.txt"])
然后我得到输出:
[[]]
我尝试用多个空格重新分割:
line = re.split(r'\s*', line)
哪个也给了
[[]]
但是预期的输出应该是:
['1', '10', '12'] ['1', '34', '44'] ['2', '1', '-3']
如何使用多个分隔符分割字符串?
尽管我看到了许多类似的问题,但是我的分隔符不是诸如“ \”或“ *”之类的特殊字符,因此,所有解决方案均无效。我正在将结果写入python中的文件,然后重新......>
for line in merged_table: line = line.strip().split() print line
这将在所有空格上分割,而不仅仅是制表符
import re
import sys, string, glob, os
with open('file1.txt') as f:
with open("control_output.txt", "w") as output:
mytext = f.read()
chapter = re.split("control[0-2]+\n", mytext)
i=1
print chapter[i]
output.write(chapter[i])
# You should move this code block out of with open("control_output.txt", "w") as output:
for filename in glob.glob(os.path.join(filePath, 'control_output.txt')):
with open(filename, 'r') as f_table:
merged_table = f_table.readlines()
for line in merged_table:
line = re.split('\s+', line.strip())
print line