简体   繁体   English

如何将两个txt文件中具有相同列的数据获取到新的txt?

[英]How to get the data with the same column in two txt file to an new txt?

I need to get over 200,000 annotations from the original annotation txt file(B.txt) by comparing the first column string.通过比较第一列字符串,我需要从原始注释 txt 文件(B.txt)中获取超过 200,000 个注释。

For example:例如:

A.txt is like A.txt 就像

00001.jpg 00001.jpg

00002.jpg 00002.jpg

00004.jpg 00004.jpg

... ...

B.txt is like B.txt 就像

00001.jpg 12 3 1 33 00001.jpg 12 3 1 33

00002.jpg 32 4 2 2 00002.jpg 32 4 2 2

00003.jpg 23 4 5 1 00003.jpg 23 4 5 1

00004.jpg 3 5 3 1 00004.jpg 3 5 3 1

00005.jpg 2 4 1 1 00005.jpg 2 4 1 1

... ...

I want get a C.txt like我想要一个 C.txt 之类的

00001.jpg 12 3 1 33 00001.jpg 12 3 1 33

00002.jpg 32 4 2 2 00002.jpg 32 4 2 2

00004.jpg 3 5 3 1 00004.jpg 3 5 3 1

... ...

The code I worte seems like can't get any line wrote in C.txt我写的代码似乎无法在 C.txt 中写入任何行

alines = open('A.txt', 'r').readlines() 
blines = open('B.txt', 'r').readlines()
fw = open('C.txt', 'w')
for al in alines:
    for bl in blines:
        if str(al) in str(bl):
            fw.write(bl)
fw.close()

Your code doesn't work because alines and blines lists contain the lines ending with the '\n' symbols so the comparison always fails.您的代码不起作用,因为alinesblines列表包含以 '\n' 符号结尾的行,因此比较总是失败。

The following code strips the '\n' symbols and also eliminates the second "for" cycle:以下代码去除了 '\n' 符号并消除了第二个“for”循环:

with open('A.txt', 'r') as fh:
    # Splitlines gets rid of the '\n' endlines
    alines = fh.read().splitlines()
with open('B.txt', 'r') as fh:
    # Splitlines gets rid of the '\n' endlines
    blines = fh.read().splitlines()
with open('C.txt', 'w') as fh:
    for line in blines:
        # Split the file name
        parts = line.split(' ', 1)
        # Look up the filename
        if parts[0] in alines:
            fh.write(line + '\n')

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

相关问题 如何从txt文件中获取写入的数据 - How to get written data from a txt file 两个如何按列写入两个嵌套列表 in.txt 文件? - How two write two nested lists column wise in .txt file? 如何从同一资源运行两个功能(绘制图形并将无限数据保存到txt文件) - How to run two function from same resource(plot graphic and saving infinite data to txt file) 如何通过python(来自txt文件)将很长的行数据分成两列 - how to split a very long row data into two column by python (from txt file) 如何依次读取两个txt文件并将其写入python中的新文件? - how to sequentially read two txt files and write into a new file in python? 如何从txt文件中提取一列并保存在新矩阵中 - How to take a column from a txt file and save in a new matrix 如何替换txt文件中的列? - How to replace a column in a txt file? 如何使用 python 保存 txt 文件并制作两列表? - How to save txt file and make two column table using python? 如何编写 python 从名为“file1.txt”的文本文件中读取前两行 将从“file1.txt”读取的两行写入新文件“file2.txt” - How write python to Read the first two lines from a text file named "file1.txt" Write the two lines read from "file1.txt" to a new file "file2.txt" 如何将已编辑的txt文件保存到新的txt文件中? - How to save an edited txt file into a new txt file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM