python - 从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"))

关于python - 从3个不同的文件中读取数据,并使用一个数据在所有文件中搜索另一个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55441241/

相关文章:

python - 如何在一行上一次打印一个单词?

python - 在 Python 中计算时间(datetime.timedelta?)

python - 按百分位数对 python 字典进行排名

python - 协程作为 Jupyter notebook 中的后台作业

Python和矩阵,移动列&行

python - 我正在使用 VS Code 通过 Python 对 EV3 进行编程,但我无法让我的红外传感器工作

python - 如何自动清除python中的变量?

python - 如何将字典转换为嵌套字典?

python - 为什么我在第二次通话后有 2 个?

python 无需步行即可检测新文件