【发布时间】:2020-11-25 23:34:13
【问题描述】:
我想在 python 中读取一个.dat 文件,我尝试了不同的方法来读取它,最后我得到了这个代码:
datContent = open("..\\data\\train.dat.abs", 'r')
MyList=[]
for line in datContent:
print(line)
以这种形式打开内容:
1 Should O
2 students O
3 be O
4 taught O
5 to O
6 compete O
7 or O
8 to O
9 cooperate O
10 ? O
------------------> THIS SHOWS, STARTING OF THE NEXT SENTENCES
1 It O
2 is O
3 always O
4 said O
5 that O
6 competition O
7 can O
8 effectively O
9 promote O
10 the O
11 development O
12 of O
13 economy O
14 . O
但我想将第一列和第二列提取为元组列表:
[(Should, O), (students,O), (be,O), (taught O), (to,O), (compete,O), (or,O), (to,O), (cooperate,O), (? O)]
每个句子(句子已在原始格式中用空格标记)是数据框的一行。我试过分裂。 我已经完成了使用:
datContent = open("..\\data\\train.dat.abs", 'r', encoding='utf-8' )
MyList=[]
for line in datContent:
a=line.split()
print(a)
结果是这样的:
['1', 'Should', 'O']
['2', 'students', 'O']
['3', 'be', 'O']
['4', 'taught', 'O']
['5', 'to', 'O']
['6', 'compete', 'O']
['7', 'or', 'O']
['8', 'to', 'O']
['9', 'cooperate', 'O']
['10', '?', 'O']
[]
['1', 'It', 'O']
['2', 'is', 'O']
['3', 'always', 'O']
['4', 'said', 'O']
['5', 'that', 'O']
['6', 'competition', 'O']
['7', 'can', 'O']
['8', 'effectively', 'O']
['9', 'promote', 'O']
['10', 'the', 'O']
['11', 'development', 'O']
['12', 'of', 'O']
['13', 'economy', 'O']
['14', '.', 'O']
如我所说,我想保存:
[(Should, O), (students,O), (be,O), (taught O), (to,O), (compete,O), (or,O), (to,O), (cooperate,O), (? O)]
作为一行数据框(基本上是上面每个列表的第 2、3 项),如您所见 [] 将发送的分开
df
row 1= [(Should, O), (students,O), (be,O), (taught O), (to,O), (compete,O), (or,O), (to,O), (cooperate,O), (? O)]
row 2= ...
等等。
【问题讨论】: