简体   繁体   English

处理txt文件中的某些行

[英]Manipulate some lines in a txt-file

I have a file with words and numbers and some file with only words. 我有一个包含单词和数字的文件,还有一个仅包含单词的文件。 First file looks like this: 第一个文件如下所示:

Cow; 39402
Horse; 2039
Pig; 2494
Snake; 39485
Monkey; 9309348

Second file looks like: 第二个文件如下所示:

Monkey
Horse
Bird
Pig
Donkey
Monkey

I want to add the numbers of the first file to the words of the second file. 我想将第一个文件的编号添加到第二个文件的字词中。 As you may see, not all items from the second file are part of the first file. 如您所见,并非第二个文件中的所有项目都属于第一个文件。 When this is the case, I want to print the word without the numbers. 在这种情况下,我要打印不带数字的单词。 So my final txt-file will look like: 因此,我最终的txt文件如下所示:

Monkey; 9309348 
Horse; 2039
Bird
Pig; 2494
Donkey
Monkey; 9309348
Cow; 39402

This is what i tried: 这是我尝试的:

f = open("wordsonly.txt", "r")
flist = f.readlines()
d = open("wordsandnumbers.txt", "r")
dlist = d.readlines()

nf = open("finalfile.txt", "w")
for line in f:
    for item in dlist:
        newitem = item.rstrip('\n')
        if newitem in line:
            splitline = newitem.split(';')
            newline = line.rstrip('\n')+';'+splitline[1]+'\n'
            nf.write(newline)
            break
        a += 1
        lengthlist = len(dlist)
        if line.rstrip('\n') not in newline:
           if a == lengthlist:
              nf.write(line)

However, this code gives in some cases too much results (more than one for each line), I have no idea why... 但是,此代码在某些情况下会给出过多的结果(每行超过一个),我不知道为什么...

(I simplified my code a bit so there may be some minor errors. I'm sorry for that) (我简化了代码,所以可能会有一些小错误。对此我感到抱歉)

First You should create dict for all words with numbers as values and words as keys 首先,您应该为所有以数字为值和以单词为键的单词创建dict

with open("wordsandnumbers.txt", "r") as file1:
    numbers = {k:v.strip() for k,v in (line.split(';') for line in file1)}

then You can just create new file writing either word (if it has no number) or word with number 那么您可以创建一个新文件,写一个单词(如果没有数字)或带有数字的单词

with open("wordsonly.txt", "r") as f:
    with open("finalfile.txt", "w") as nf:
        for line in f:
            line = line.strip():
            if line in numbers:
                nf.write('{}; {}\n'.format(line, numbers[line])
            else:
                nf.write('{}\n'.format(line))

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

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