操纵我有一个文本文件:从文件中读取,并在Python
它可以在每个时间和行数改变可以改变的,并且包含每个行:
string (can contain one word, two or even more)^string of one word
EX:
level country^layla
hello sandra^organization
hello people^layla
hello samar^organization
我想用大熊猫来创建数据帧这样的:
item0 (country, people)
item1 (sandra , samar)
因为例如每次出现蕾拉,我们正在返回属于它,增加一条,作为第二列只是展示的具有最正确的名称在这种情况下(国家,人民),我们把layla称为item0,并将其作为数据框的索引。我似乎无法安排这一点,我不知道如何做的逻辑返回“^”后的任何重复和返回其属于最正确的名称列表。我的审判至今它并没有真正做到这一点是:
def text_file(file):
list=[]
file_of_text = "text.txt"
with open(file_of_context) as f:
for l in f:
l_dict = l.split(" ")
list.append(l_dict)
return(list)
def items(file_of_text):
list_of_items= text_file(file_of_text)
for a in list_of_items:
for b in a:
if a[-1]==
def main():
file_of_text = "text.txt"
if __name__ == "__main__":
main()
现在您已经添加了更多的文本文件的新列,你想要的输出是什么? – Abdou