当前格式:我有一个ml.transcription文件,它以以下方式由句子组成-(在句子的开头和结尾都有标签,后面跟一个序列id)
1. <r>The quick brown fox jumps over a lazy dog </r> (umnle_001_001)
2. <r> I think we should go get it now </r> (umnle_001_002)
3. ......................................................
4. <r> When I travel, I prefer to travel by air </r> (umnle_001_129)
5. <r> The law was changed </r> (umtci_001_001)
6. <r> This soup needs more salt </r> (umtci_001_002)
7. .................................................
8. .................................................
9. <r> Tom sat two rows ahead of me </r> (umtci_001_197)所需格式:我需要将所需结果保存在名为newml.transcription的新文件中,转换后所需格式如下-
1. umnle_001_001 The quick brown fox jumps over a lazy dog
2. umnle_001_002 I think we should go get it now
3. ......................................................
4. umnle_001_129 When I travel, I prefer to travel by air
5. umtci_001_001 The law was changed
6. umtci_001_002 This soup needs more salt
7. ......................................................
8. ......................................................
9. umtci_001_197 Tom sat two rows ahead of me#!/usr/bin/env python
fo = open(" ml.transcription", "r")
y_list = []
for line in fo.readlines():
a1 = line [-15:-2]
a2 = line [4:]
y = str(a1)+ " "+ str(a2)
a3 = y[:-22]
y_list.append(a3)
print(a3)
fo.close()
fo = open("newml.transcription", "w")
for lines in y_list:
fo.write(lines,"\n")
fo.close()我收到的错误:
Fo.write(行,"\n")
TypeError:函数只接受1个参数(给定2个)
虽然上述代码中的逻辑是正确的,并打印出所需的输出,但此错误会预先创建一个problem.Thanks。
发布于 2017-02-04 18:20:56
这是一种粗糙的方式:
import re
with open("input", "r") as input:
for line in input:
print line.split("</r> ")[1][2:-2] + " " + line.split("r>")[1][1:-3]上面打印到屏幕上,您可以通过管道将其输出到一个文件中。它假设在<r>之后以及在</r>之前和之后总是有一个空格。它还假定每行都以换行符结束。
https://stackoverflow.com/questions/42038338
复制相似问题