[英]How to read different .txt file columns in different strings in Python?
I have the following .txt
file which is called answers.txt
: 我有以下
.txt
文件,称为answers.txt
:
0 identify
0 organizations
0 that
0 participate
0 in
0 international
0 criminal
0 activity
0 the
0 activity
0 and
0 if
0 possible
0 collaborating
0 organizations
0 and
0 countries
0 involved
1 is
1 the
1 disease
1 of
1 poliomyelitis
1 polio
1 under
1 control
1 in
1 the
1 world
The first column plays the role id
, that means, columns with the same id
belong to the same sentence as follows: 第一列扮演
id
的角色,这意味着具有相同id
列属于同一句子,如下所示:
answer_0 = 'identify organizations that participate in international criminal activity and if possible collaborating organizations and countries involved'
answer_1= 'is the disease of poliomyelitis polio under control in the world'
So far I have been able to read each line of my document by using the following code: 到目前为止,通过使用以下代码,我已经能够阅读文档的每一行:
separator=' '
string=[]
for line in open("answers.txt"):
columns = line.split(separator)
if len(columns) >= 2:
print (columns[1])
But I don't want the words belonging to the same sentence separate but together in the same string as in answer_0
and in answer_1
. 但是我不希望将属于同一句子的单词分开,而是将它们与
answer_0
和answer_1
放在同一字符串中。 Ideally, I would like to have list=[answer_0, answer_1]
. 理想情况下,我希望有
list=[answer_0, answer_1]
。
If I understood you correctly, I suggest you read the id at the beginning of each line and store the strings in a dictionary. 如果我对您的理解正确,建议您阅读每一行开头的ID,并将字符串存储在字典中。 Like so:
像这样:
answer_dict = {}
for line in open("answers.txt"):
line_values = line.split()
try:
answer_dict[int(line_values[0])] += " " + line_values[1]
except:
answer_dict[int(line_values[0])] = line_values[1]
And then you can do whatever you want with the dictionary. 然后,您可以使用字典进行任何操作。 To make it into a list:
使其成为列表:
answer_list = []
for id in answer_dict.keys():
answer_list += answer_dict[id]
What you seem to be looking for is in the lines of : 您似乎在寻找以下方面的内容:
def fileReader(filename):
f_obj = open(filename,"r")
table_dict = {}
seperator = " "
for line in f_obj:
id, word = line.split(seperator)
existing_list = table_dict.get("answer_"+id, "")
existing_list += " " + word
table_dict["answer_"+id] = existing_list
return table_dict
I think there is no need for dictionary. 我认为不需要字典。 When you split the row, it creates a list of sub strings where 1st element would be the column number indicating the sentence number and another element would have the substring of your sentence.
拆分行时,它将创建一个子字符串列表,其中第一个元素是指示句子编号的列号,另一个元素将包含句子的子字符串。 So you can generate your sentence on the go which would save the Space Complexity needed by Dictionary and maybe somewhat faster too.
因此,您可以随时随地生成句子,这将节省Dictionary所需的空间复杂度,并且也许还会更快。
separator=' '
string=[]
for line in open("answers.txt"):
columns = line.split(separator)
if columns[0]== '0':
answer_0 += " "+ columns[1]
elif columns[0]== '1':
answer_1 += " "+ columns[1]
You can construct the sentences on the fly. 您可以动态构建句子。 For example:
例如:
sentences = dict()
for line in open('answers.txt'):
n, word = line.split(' ')
sentences.setdefault(n, []).append(word)
Then each sentence has a key in sentences
and is a list of words, you can join them, for example for the sentence whose id is 1: 然后,每个句子有一个关键
sentences
,是单词的列表,你可以加入他们的行列,例如用于id为1的句子:
' '.join(sentences[1])
For all sentences: 对于所有句子:
for n, words in sentences.items():
print(' '.join(words))
Try this: 尝试这个:
columns = []
string1 = []
string2 = []
for line in open("answers.txt"):
columns = line.split(separator)
if columns[0] == “0”:
string1.append(columns[1])
else:
string2.append(columns[1])
answer1 = ‘’.join(string1)
answer2 = ‘’.join(string2)
print answer1
print answer2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.