简体   繁体   English

使用python将文本文件中的行附加到另一个文本文件

[英]Appending line from text file to another text file with python

I am working on some file operations with python.我正在使用 python 进行一些文件操作。

I have two text files.我有两个文本文件。 First, contains a lot of lines about bigram word embedding results such as apple_pie 0.3434 0.6767 0.2312 .首先,包含很多关于apple_pie 0.3434 0.6767 0.2312词嵌入结果的行,例如apple_pie 0.3434 0.6767 0.2312 And another text file which contains a lot of lines with unigram word embedding results of apple_pie has apple 0.2334 0.3412 0.123 pie 0.976 0.75654 0.2312 I want to append apple_pie bigram word embedding results with apple and pie unigram so it result becomes something like: apple_pie 0.3434 0.6767 0.2312 0.2334 0.3412 0.123 0.976 0.75654 0.2312 in one line.另一个文本文件包含很多带有apple_pie has apple 0.2334 0.3412 0.123 pie 0.976 0.75654 0.2312 unigram 词嵌入结果的apple_pie has apple 0.2334 0.3412 0.123 pie 0.976 0.75654 0.2312我想用apple 和pie unigram 附加apple_pie bigram 词嵌入结果,这样结果就变成了: apple_pie 0.3434 0.6767 0.2312 0.2334 0.3412 0.123 0.976 0.75654 0.2312一行。 Does anybody know how to do this?有人知道怎么做这个吗? Thanks...谢谢...

bigram = open("bigram.txt",'r')
unigram = open("unigram.txt",'r')
combine =open("combine.txt",'w')
bigram_lines = bigram.readlines()
unigram_lines = unigram.readlines()
iteration = 0
while iteration < len(bigram_lines):
    num_list_bigram = []
    text_list_bigram = []
    for item in bigram_lines[iteration].split(" "):
        if "." in item:
            num_list_bigram.append(item)
        else:
            text_list_bigram.append(item)
    num_list_unigram = []
    text_list_unigram = []
    for item in unigram_lines[iteration].split(" "):
        if "." in item:
            num_list_unigram.append(item)
        else:
            text_list_unigram.append(item)
    iteration+=1
    com_list=text_list_bigram+num_list_bigram+num_list_unigram
    for item in com_list:
        combine.write(item+" ")
    combine.write("\n")
bigram.close()
unigram.close()
combine.close()

Hopefully this will work for you希望这对你有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM