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

时间:2019-03-31 13:12:55

标签: python

我有三个用于读取文本的文件。每个文件包含两个不同的数据点。

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

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

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

我敢肯定必须有一个更好的方法来解决这个问题,但是我不确定我是否知道使我到达那里的工具...

到目前为止,这是我所得到的(我为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)

1 个答案:

答案 0 :(得分:0)

这是解决此问题的一个示例。此示例既不高性能,也不具有可伸缩性,因为它不使用任何索引进行查找,它只是遍历所有条目以查找匹配的条目。

如果您希望此“过程”表现出色,建议您使用数据库。

# 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"))