简体   繁体   English

如何在Python中以不同的字符串读取不同的.txt文件列?

[英]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_0answer_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.

相关问题 如何从 python 中的不同目录读取 .txt 文件? - How do I read a .txt file from a different directory in python? 如何在python中从txt中选择不同的列 - how to select different columns from txt in python 如何读取一些不同风格的txt文件 - How to read txt file with some different style Python 如何读取不同行列的文本文件? - Python how to read text file with different rows for columns? 如何在Python代码中传递txt文件中的新行以将函数应用于不同的字符串? - How to pass new lines in txt file in Python code to apply function to different strings? 如何在txt文件中添加两个列表,以便它们留在两个不同的列中并用逗号分隔? (蟒蛇) - how can i add two lists in a txt file so they stay in two different columns and are separated by a comma? (Python) 从文件中获取不同的字符串并写入 a.txt - Get different strings from a file and write a .txt 如何在Python中合并来自不同txt文件的一些列数据? - how to merge some columns data from different txt files in Python? 使用Python Pandas读取.txt文件-字符串和浮点数 - Read .txt file with Python Pandas - strings and floats Python:从.txt中提取位置相关的字符串并将它们保存到dataframe的不同列中 - Python: extract position-dependent strings from .txt and save them to different columns of a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM