[英]Merging several text files line by line in python
我是一名网络工程师,刚接触 Python,这个问题将应用于路由器上的访问列表,但为简单起见,我将使用州和城市。
我有几个文本文件(下面两个),其中包含如下所示的州和城市行:
文件 1
Texas
Austin
Dallas
Houston
San Antonio
文件 2
Texas
Amarillo
Austin
Dallas
San Antonio
Waco
我需要合并这两个文件并生成一个新的文本文件,如下所示:
Texas
Amarillo
Austin
Dallas
Houston
San Antonio
Waco
定位必须如此精确,因为与 file2 相比,file1 缺少 Amarillo,并且 file2 在 Austin 之上有 Amarillo,然后合并的文件将在结果文件中将 Amarillo 放在 Austin 之上或德克萨斯州之下。 如果与 file1 相比,file2 缺少某些城市,则同样适用。
我不太确定如何启动这个脚本。 指导手将不胜感激!
谢谢!
这是一种简单的方法:
#! /usr/bin/python3
from sys import exit
def w(data, title):
with open('f3.txt', 'w') as file_out:
file_out.write(title + '\n')
for line in data:
file_out.write(line + '\n')
def r(path):
with open(path) as file_in:
lines = file_in.read().split('\n')
return [l for l in lines if l]
def combine(path1, path2):
f1 = r(path1)
f2 = r(path2)
title1 = f1.pop(0)
title2 = f2.pop(0)
# ensure Texas is the first line in each file
if title1 != title2:
print("Titles do not match")
exit()
w(sorted(set(f1 + f2)), title1)
if __name__ == "__main__":
combine('f1.txt', 'f2.txt')
这是运行前后的目录/文件内容:
james@rootVIII:~/Desktop$ ls
delete f1.txt f2.txt test.py utils
james@rootVIII:~/Desktop$
james@rootVIII:~/Desktop$ cat f1.txt
Texas
Austin
Dallas
Houston
San Antonio
james@rootVIII:~/Desktop$
james@rootVIII:~/Desktop$ cat f2.txt
Texas
Amarillo
Austin
Dallas
San Antonio
Waco
james@rootVIII:~/Desktop$
james@rootVIII:~/Desktop$ ./test.py
james@rootVIII:~/Desktop$
james@rootVIII:~/Desktop$
james@rootVIII:~/Desktop$ cat f3.txt
Texas
Amarillo
Austin
Dallas
Houston
San Antonio
Waco
一些注意事项:
这期望“Texas”或州名是每个文本文件(f1.txt 和 f2.txt)中的第一个条目
将列表变成集合会删除重复项
combine() 方法可以接受相对或绝对路径
列表推导[l for l in lines if l]
返回一个没有空元素的列表(因为字符串被换行符分割了)......如果在空格上分割,你将得到 San 而不是 San Antonio
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.