Python3,似乎无法理解如何从一个文件中获取信息并将其分发给其他几个文件

2024-09-28 20:17:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个任务,我必须收集大量存在于txt文件中的信息并对它们进行排序。这些信息围绕着个人信息,并按顺序排列。例如 姓姓 社会保险号码

地址

等等

我的目标是先把这些信息整理出来,这样我就可以按顺序把它们打印出来,然后决定所提供的虚构人物信息的性别。你知道吗

我想把文件里的信息整理一下

  • 一张只有名字的单子
  • 一份只有社会保险号码的名单
  • 一个只有地址的列表

然后使用某种zip函数将列表上的索引添加到一起,以便创建概要文件。这样,我就可以查看社会安全号码,并显示此人的性别。你知道吗

到目前为止我想出的代码是

with open ("names.txt", "r", encoding="utf-8" ) as allasinfo:
namn = []
personnummer = []
adress = []
for x in allasinfo.readlines():
    i = 3
    while i > 0:


        namn.append(x)
        i-=1

        personnummer.append(x)
        i-=1

        adress.append(x)
        i-=1

我在这里的计划是,程序将把文件中的第一个索引移到名字的第一个列表中,第二行是社会保障等等。。程序打印出所有3个列表中的所有信息,使它们完全相同。有没有办法像我尝试的那样把一个文件中的信息分成3个不同的位置?你知道吗


Tags: 文件程序txt信息列表地址名字号码
4条回答

我假设您的文本文件有标题,称为“name”、“personid”和“address”。你知道吗

import pandas as pd
file = pd.read_csv("fpath/file.txt", sep=" ")
namn = []
personnummer = []
adress = []
for row in file.itertuples():
    namn.append(row.name)
    personnummer.append(row.personid)
    adress.append(row.address)

这就是你想做的吗?你知道吗

有几种方法可以解决这样的问题。听起来您的文本文件包含以下原始数据:

Name One
1111111
111 One Street
Name Two
2222222
222 Two Street
Name Three
3333333
333 Three Street

如果是这样的话,您要做的是在读取数据时跟踪行号,然后根据行号为每一行指定一个值。我倾向于将结果数据存储在不同的数组中,而不是将其存储在字典元素的数组中,因此可以将每个数据单元视为一个集合,这使得处理数据更直接,并且意味着不需要将多个数组传递给不同的助手函数。你知道吗

values = []

with open("names.txt", "r", encoding="utf-8") as allasinfo:
    line_number = 0
    # The target keys in each dictionary object we'll create
    target_keys = ['name', 'number', 'address']
    # Just use the file object's built in enumerator
    for x in allasinfo:
        # if this line number starts a new record set, add
        # a dictionary to the list of values
        if line_number % len(target_keys) == 0:
            values.append({})
        # And add this row to the last dictionary, using the line
        # number to note which key type this is
        values[-1][target_keys[line_number % len(target_keys)]] = x
        # Increment the line number
        line_number += 1

这将把值存储在一个名为values的数组中,每个数组有三个条目。如果您的文本文件没有包含足够的行,那么最后一个元素将丢失一些条目,因此请确保处理这种情况。你知道吗

我假设您的文本文件有标题,称为“name”、“personid”和“address”。你知道吗

import pandas as pd
file = pd.read_csv("fpath/file.txt", sep=" ")
namn = []
personnummer = []
adress = []
for row in file.itertuples():
    namn.append(row.name)
    personnummer.append(row.personid)
    adress.append(row.address)

这就是你想做的吗?你知道吗

有几种方法可以解决这样的问题。听起来您的文本文件包含以下原始数据:

Name One
1111111
111 One Street
Name Two
2222222
222 Two Street
Name Three
3333333
333 Three Street

如果是这样的话,您要做的是在读取数据时跟踪行号,然后根据行号为每一行指定一个值。我倾向于将结果数据存储在不同的数组中,而不是将其存储在字典元素的数组中,因此可以将每个数据单元视为一个集合,这使得处理数据更直接,并且意味着不需要将多个数组传递给不同的助手函数。你知道吗

values = []

with open("names.txt", "r", encoding="utf-8") as allasinfo:
    line_number = 0
    # The target keys in each dictionary object we'll create
    target_keys = ['name', 'number', 'address']
    # Just use the file object's built in enumerator
    for x in allasinfo:
        # if this line number starts a new record set, add
        # a dictionary to the list of values
        if line_number % len(target_keys) == 0:
            values.append({})
        # And add this row to the last dictionary, using the line
        # number to note which key type this is
        values[-1][target_keys[line_number % len(target_keys)]] = x
        # Increment the line number
        line_number += 1

这将把值存储在一个名为values的数组中,每个数组有三个条目。如果您的文本文件没有包含足够的行,那么最后一个元素将丢失一些条目,因此请确保处理这种情况。你知道吗

相关问题 更多 >