简体   繁体   English

将文本文件读入字典,以供以后添加/修改/删除

[英]Read text file into dictionary to be used later for adding/modifying/deleting

Let me preface by saying I'm not 100% sure if using a dictionary is the best course of action for this task but that is what I believe I need to use to accomplish this. 首先,请允许我说我不确定100%是否使用字典是完成此任务的最佳方法,但是我认为这是完成此任务所需要的。

I have a .txt file that is formatted like this: 我有一个.txt文件,其格式如下:

first_name last_name rate hours
first_name last_name rate hours
first_name last_name rate hours
first_name last_name rate hours

There is a single space between each item. 每个项目之间只有一个空格。 Each line represents a person. 每行代表一个人。

For my program I need to be able to: 对于我的程序,我需要能够:

  • print out all the people at once 一次打印出所有人
  • be able to search for a person by first or last name and print out their information 能够通过名字或姓氏搜索人并打印其信息
  • modify a person (first name, last name, hours, rate) 修改一个人(名字,姓氏,时间,费用)
  • delete a person (all their information) 删除一个人(所有信息)

When it gets printed I DO NOT need to see the [rate] and [hours] but [gross pay] instead (gross pay = rate * hours). 当它被印我不需要看[费用]和[时间],但[工资总额]而不是(工资总额=率*小时)。

I am fairly new to file processing with python so my first attempt at this was just to read every line from the file and print it out on the screen, but I came across the problem of being able to display [gross pay]. 我对使用python进行文件处理非常陌生,因此我的第一个尝试只是从文件中读取每一行并将其打印在屏幕上,但是遇到了能够显示[总薪水]的问题。

# 'print_emp', display only a single employee's data chosen by the user displayed as
# firstname, lastname, grosspay (on one line of output)
def print_emp():
    menu_name = ' '*int(OFFSET/2) + "EMPLOYEE LOOKUP"
    dotted = (OFFSET+len(menu_name))*'-'

    try:
        with open('employees.txt') as file:
            print('{} \n{} \n{}'.format(dotted, menu_name, dotted))
            emp_name = input("Employee Name: ")
            print('{0:20} {1:20} {2}'.format("First Name", "Last Name", "Gross Pay"))
            for line in file:
                if emp_name in line:
                    print (line.strip())

                #print("\nEmployee", emp_name, "does not exist. Try again.\n")
                #break
    except FileNotFoundError:
        print("Error: File not found.")


# 'print_all_emp', display all employee data in format firstname, lastname,
# grosspay (on one line of output per employee)
def print_all_emps():
    menu_name = ' '*int(OFFSET/2) + "EMPLOYEE LIST"
    dotted = (OFFSET+len(menu_name))*'-'

    try:
        with open('employees.txt', 'r') as file:
            print('{} \n{} \n{}'.format(dotted, menu_name, dotted))
            print('{0:20} {1:20} {2}'.format("First Name", "Last Name", "Gross Pay"))
            for line in file:
                print(line.strip())
            print(dotted)
    except FileNotFoundError:
        print("Error: File not found.")

I am not sure how I go about reading my .txt file into a dictionary (if that's what I need to do) where I assign a key to each person that includes their first name, last name, rate, and hours and then multiplying the rate * hours to create the gross pay and then displaying that gross pay. 我不确定如何将.txt文件读入字典(如果需要这样做),我会在其中为每个人分配一个密钥,其中包括他们的名字,姓氏,比率和小时数,然后将其乘以费率*小时以创建总工资,然后显示该总工资。

I will be creating three more functions where I can add, delete, and modify the people in the .txt file. 我将创建另外三个函数,可以在其中添加,删除和修改.txt文件中的人员。

EDIT : 编辑:

I believe what I am going for as an end program looks something like this: 我相信最终程序的目标如下:

https://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3/File_IO https://zh.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3/File_IO

But without the load and save functions... 但是没有加载和保存功能...

Presuming you have space delimited data, you can just use the csv library. 假设您有用空格分隔的数据,则可以只使用csv库。

import csv

labels = ['first_name', 'last_name', 'rate', 'hours']
data = csv.DictReader(open('./test.txt'), delimiter=' ', fieldnames=labels)

result = []

for row in data:
  result.append(row)

print result

You will wind up with an array of dictionaries that each have the labels as key names. 您将获得一系列字典,每个字典都具有标签作为键名。

i think the problem you are facing is how to find unique key 我认为您面临的问题是如何找到唯一的密钥
to create unique key, simply add all string together than hash it. 要创建唯一键,只需将所有字符串加在一起而不是对其进行哈希处理。

res = {}
with open('employees.txt') as file:
   for line in file:
       res[line] = line.split(' ')

You make some of the code easier by creating a Person class. 通过创建Person类,可以使某些代码更容易。

class Person:
    def __init__(self, first, last, rate, hours):
       self.first = first
       self.last = last
       self.rate = rate
       self.hours = hours

    def matches_name(self, name):
        return name.lower() == self.first.lower() or name.lower() == self.last.lower()

    def __str__(self):
        return '{} {} {}'.format(self.first, self.last, self.rate*self.hours)

This will simplify your code a bit. 这将简化您的代码。 If you want to find out if someone has a specific name, you can just call something like: 如果您想知道某人是否有特定名称,您可以致电:

a_person.matches_name(random_first_name)

If you want to print out the person and their gross pay, you just have to do 如果要打印此人及其工资,您只需要做

print(a_person)

You can easily achieve all the desired operations with pandas : 您可以使用pandas轻松实现所有所需的操作:

>>> import pandas as pd

First convert your file to csv format (comma separated text file), then: 首先将文件转换为csv格式(逗号分隔的文本文件),然后:

assuming your file is in txt: 假设您的文件为txt:

>>> txt = """                
    'FN','LN','rate','hours'
    'John','Doe','12','40'
    'Jane','Roe','20','35'
    """

>>> file = StringIO(txt)

>>> df = pd.read_csv(file, quotechar="'")

>>> df
Out: 
     FN   LN  rate  hours
0  John  Doe    12     40
1  Jane  Roe    20     35

print out all the people at once: 一次打印出所有人员:

>>> df[['FN','LN']]
Out: 
     FN   LN
0  John  Doe
1  Jane  Roe

Search for a person by first or last name and print out their information: 通过名字或姓氏搜索人并打印其信息:

>>> df.loc[df['FN'] == 'John']
Out: 
     FN   LN  rate  hours
0  John  Doe    12     40

>>> df.loc[df['LN'] == 'Roe']
Out: 
     FN   LN  rate  hours
1  Jane  Roe    20     35

Modify a person (first name, last name, hours, rate): 修改一个人(名字,姓氏,小时,费率):

I'll show modifying the rate for a person whose name is 'John' 我将展示如何修改姓名为“约翰”的人的费率

>>> df.loc[df['FN'] == 'John', 'rate'] = 14

>>> df
Out: 
     FN   LN  rate  hours
0  John  Doe    14     40
1  Jane  Roe    20     35

Delete a person (all their information): 删除一个人(所有信息):

I'll show deleting 'John Doe': 我将显示删除“ John Doe”:

>>> df = df[(df['FN'] != 'John') & (df['LN'] != 'Doe')]

>>> df
Out: 
     FN   LN  rate  hours
1  Jane  Roe    20     35

Finally, if you want the gross pay you can add a new column pay that holds that information: 最后,如果您想要总工资,则可以添加一个包含该信息的新列pay

>>> values = df['rate'] * df['hours']

>>> df['pay'] = values

>>> df
Out[67]: 
     FN   LN  rate  hours  pay
0  John  Doe    12     40  480
1  Jane  Roe    20     35  700

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM