从 3 个不同的文件中读取数据,并使用一条数据在所有文件中搜索另一条数据

  • 本文关键字:数据 文件 一条 搜索 读取 python
  • 更新时间 :
  • 英文 :


我有三个不同的文件可以从中读取文本。每个文件包含两个不同的数据点。

例如,第一个文件包含姓名和电话号码,第二个文件包含姓名和社会,第三个文件包含社会和收入。

我希望用户能够输入电话号码,并且程序能够吐出与该号码相关的人的所有其他已知数据(即社交,姓名,收入)。

我输入了文件并创建了 4 个不同的列表,然后我的想法是告诉程序类似"如果电话号码在'电话'列表中,请从下一个列表中获取相应的索引值,依此类推每个列表。不过,我的部分问题是,我不一定在每个电话号码的每个列表中都有相应的值,所以我不确定使用列表的索引是最好的方法,因为索引值不一定会配对。

我确信必须有更好的方法来解决这个问题,但我只是不确定我是否知道有什么工具可以让我到达那里......

这就是我到目前为止得到的(我有类似的 data2 和 data3 代码块,但为了简洁起见,没有包括):

data1 = open("data1.txt", "r")
data2 = open("data2.txt", "r")
data3 = open("data3.txt", "r")
names = []
phones = []
socials = []
incomes = []
for line in data1: 
if "," in line:
parts = line.split(",")
name = parts[0]
if name in names:
names = names
else:
name = name.strip()
names.append(name)
phone = parts[1]
phone = phone.strip()
phones.append(phone)

下面是如何解决此问题的示例。此示例既不具有性能,也不可扩展,因为它不使用任何索引进行查找,它只是遍历所有条目以查找匹配的条目。

如果您希望此"过程"具有性能,我建议考虑使用数据库。

# A method for loading the file, which accepts a list of "headers" (or keys) 
# to be used in order to understand what data is in each file. 
# It collects all the data in the entries.
def load_file(entries, filename, keys):
with open(filename, "r") as file:
for line in file:
# clean up and split by comma
values = line.strip().split(',')
# transform values into [("name", "a name"), ("phone", "11111111")]
pairs = zip(keys, values)
# transform pairs into {"name": "a name", "phone": "11111111"}
entry = dict(pairs)
update_or_insert(entries, entry)
def update_or_insert(entries, new_entry):
# go through all entries
for index, entry in enumerate(entries):
# we need to check for matching values of each key of new_entry
# so iterate through the items of new_entry
for key, value in new_entry.items():
# if a key of new_entry exists in an already existing entry
# and their values are equal
if key in entry and entry[key] == value:
# merge entry and new_entry
new_entry = {**entry, **new_entry}
# update entry with the new merged one
entries[index] = new_entry
# exit the search
return
# if no matching values for new_entry keys were found,
# then insert new_entry into entries
entries.append(new_entry)
def retrieve(entries, key, value):
for entry in entries:
if key in entry and entry[key] == value:
return entry
entries = []
load_file(entries, "data1.txt", ["name", "phone"])
load_file(entries, "data2.txt", ["name", "social"])
load_file(entries, "data3.txt", ["social", "income"])
print(entries)
print(retrieve(entries, "income", "2000"))
print(retrieve(entries, "name", "a name"))
print(retrieve(entries, "social", "non existent"))

最新更新