简体   繁体   English

从 csv 文件读取/写入字符串、整数和字符串列表的字典

[英]read/write dictionary of strings, integers, and lists of strings from/to csv file

I have tried searching this problem but can only find strings, ints, or lists individually and those solutions seem to clash when I attempt to combine them.我试过搜索这个问题,但只能单独找到字符串、整数或列表,当我尝试组合它们时,这些解决方案似乎会发生冲突。

I am writing a program that assigns chores to users taking in account for: user availability, amount they can do in a day, any chores they cannot do, last chore(s) they were assigned, and the previous week of assignments.我正在编写一个程序,为用户分配家务,考虑到:用户可用性、他们一天可以做的数量、他们不能做的任何家务、他们分配的最后一个家务以及前一周的作业。 I am still working on data structure, but so far it seems like using multiple csv files, lists, and dictionaries is the way to go.我仍在研究数据结构,但到目前为止,似乎使用多个 csv 文件、列表和字典是 go 的方法。 I thought about using classes, but I am unsure how to go about that.我考虑过使用类,但我不确定如何使用 go。

I am using a combination of the csv and pandas modules to read/write to files.我正在使用 csv 和 pandas 模块的组合来读取/写入文件。 I have found the csv module (for the most part) writes to the file in the format I want, and the pandas module (again for the most part) reads the files the way I want.我发现 csv 模块(在大多数情况下)以我想要的格式写入文件,并且 pandas 模块(在大多数情况下)以我想要的方式读取文件。

Here is my csv file called users.csv (eventually the LastChore column will be populated by either a single chore, or a list of chores, or maybe I may just remove it and use a different dictionary):这是我的名为 users.csv 的 csv 文件(最终 LastChore 列将由单个家务或家务列表填充,或者我可能只是删除它并使用不同的字典):

User用户 Available可用的 DayMax DayMax Restrict严格 LastChore最后的家务
Jer杰尔 ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] [“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”] 1 1 None没有任何 None没有任何
Bob鲍勃 ['Tue', 'Wed', 'Fri', 'Sat', 'Sun'] ['周二','周三','周五','周六','周日'] 1 1 garbage垃圾 None没有任何
Sara萨拉 ['Wed', 'Thu', 'Fri'] ['星期三','星期四','星期五'] 1 1 None没有任何 None没有任何
Kelly凯利 ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] [“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”] 2 2 vacuum真空 None没有任何

Here is the resulting list of dictionaries:这是字典的结果列表:

[{'User': 'Jer', 'Available': "['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']", 'DayMax': 1, 'Restrict': 'None', 'LastChore': 'None'}, {'User': 'Bob', 'Available': "['Tue', 'Wed', 'Fri', 'Sat', 'Sun']", 'DayMax': 1, 'Restrict': 'garbage', 'LastChore': 'None'}, {'User': 'Sara', 'Available': "['Wed', 'Thu', 'Fri']", 'DayMax': 1, 'Restrict': 'None', 'LastChore': 'None'}, {'User': 'Kelly', 'Available': "['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']", 'DayMax': 2, 'Restrict': 'vacuum', 'LastChore': 'None'}] [{'User': 'Jer', 'Available': "['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']", 'DayMax': 1, 'Restrict': 'None', 'LastChore': 'None'}, {'User': 'Bob', 'Available': "['Tue', 'Wed', 'Fri', 'Sat', 'Sun']", 'DayMax': 1, 'Restrict': 'garbage', 'LastChore': 'None'}, {'User': 'Sara', 'Available': "['Wed', 'Thu ', 'Fri']", 'DayMax': 1, 'Restrict': 'None', 'LastChore': 'None'}, {'User': 'Kelly', 'Available': "['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']", 'DayMax': 2, 'Restrict': 'vacuum', 'LastChore': 'None'}]

As you can see, my lists are enclosed with "".如您所见,我的列表用“”括起来。

UPDATE:更新:

I solved the following issue by adding "index=False" to the to_csv line.我通过在 to_csv 行中添加“index=False”解决了以下问题。

dataframe.to_csv('users.csv',index=False)

I will leave the rest here for anyone else having that problem.我会将 rest 留在这里,以供其他遇到此问题的人使用。 I am still having an issue with my lists within the dictionaries having "" around them though.我仍然对我的字典中的列表有问题,但它们周围有“”。

On top of this, after running the program it keeps adding columns to my file each time I run it.最重要的是,在运行该程序后,它会在每次运行它时不断向我的文件中添加列。

This is the result of the csv file after running it 4 times:这是 csv 文件运行 4 次后的结果:

Unnamed: 0.2未命名:0.2 Unnamed: 0.1未命名:0.1 Unnamed: 0未命名:0 User用户 ... ...
0 0 0 0 0 0 0 0 Jer杰尔 ... ...
1 1 1 1 1 1 1 1 Bob鲍勃 ... ...
2 2 2 2 2 2 2 2 Sara萨拉 ... ...
3 3 3 3 3 3 3 3 Kelly凯利 ... ...

And the resulting dictionaries:以及由此产生的字典:

[{'Unnamed: 0.2': 0, 'Unnamed: 0.1': 0, 'Unnamed: 0': 0, 'User': 'Jer',... [{'未命名:0.2':0,'未命名:0.1':0,'未命名:0':0,'用户':'Jer',...

Here is my code:这是我的代码:

import csv
import pandas

#Loading user data from files and creating dictionaries.
def loadUsers():
    try:
        dataframe = pandas.read_csv('users.csv')
        userList = dataframe.to_dict('records')
        dataframe.to_csv('users.csv')
    except FileNotFoundError:
        print("Users database missing. Creating file")
        makeFile("users.csv")
    except Exception as e:
        print("Problem with files")
    return userList

NOTE: I have been working with the loadUsers function and editing the csv in excel.注意:我一直在使用 loadUsers function 并在 excel 中编辑 csv。 Currently I have a makeFile function that creates the csv file with the header row only using csv module, I have yet to write the (newer) code that writes all this information to the csv file. Currently I have a makeFile function that creates the csv file with the header row only using csv module, I have yet to write the (newer) code that writes all this information to the csv file. Originally I was using text files, now switching code over to csv files.最初我使用的是文本文件,现在将代码切换到 csv 文件。

I ended up fixing my problem by using ast.literal_eval() on each list that was coming out as a string.我最终通过在每个以字符串形式出现的列表上使用 ast.literal_eval() 来解决我的问题。

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

相关问题 将列表写入pandas dataframe到csv,从csv读取dataframe并再次转换为列表而没有字符串 - write lists to pandas dataframe to csv, read dataframe from csv and convert to lists again without having strings 用整数替换字典中的字符串(来自文本文件) - Replace strings in a dictionary with integers (from a text file) 将作为字符串列表的字典值转换为整数列表 - Converting dictionary values that are lists of strings to lists of integers python-列表以字符串形式从csv中读取 - python - lists are being read in from csv as strings 从带有字符串和整数的元组列表写入文件 - Write to file from list of tuples with strings and integers 如何将字节作为字节字符串而不是整数写入csv文件? - How to write bytes to csv file as byte strings not integers? 为什么 csv 文件中的整数作为字符串读入 pandas dataframe - Why Integers in csv file read as strings into pandas dataframe 将 csv 文件读入列表并将字符串转换为整数 Python - Read csv file into list and convert the strings into integers Python Python字符串和整数写入文件? - Python strings and integers to write to file? 如何在不将整数列表转换为字符串的情况下将 csv 文件中的整数列表读入 python? - How to read list of integers from a csv file into python without converting them to strings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM