在python中,纯文本文件可以表示为序列。考虑
plain.txt
以下:
This is the first line!\n
This is the second line!\n
This is the third line!\n
你可以使用
with
保留字以创建管理打开/关闭逻辑的上下文,如下所示:
with open("./plain.txt", "r") as file:
for line in file:
# program logic
pass
"r"
指open使用的模式。
因此,使用这个习惯用法,您可以以适合您的文件访问模式的方式存储重复值,并在遇到重复值时忽略它。
编辑:我看到你的编辑,看起来这实际上是一个csv,对吧?如果是的话,我推荐熊猫套餐。
import pandas as pd # Conventional namespace is pd
# Check out blob, os.walk, os.path for programmatic ways to generate this array
files = ["file.csv", "names.csv", "here.csv"]
df = pd.DataFrame()
for filepath in files:
df = df.append(pd.read_csv(filepath))
# To display result
print(df)
# To save to new csv
df.to_csv("big.csv")