读取、拆分和附加 csv 到 python 中的列表
Read, Split and Append csv to lists in python
问题
我需要构建一个后端函数来读取 csv 文件中的数据,拆分值并将它们附加到多个列表中。列表中 adding/removing/displaying 值的程序正在运行,但现在我需要添加文件 I/O.
我有文件 inventory.csv 并且有一个函数 load_file_data() 用于读取、拆分然后将数据插入到三个不同的列表中,这些列表通过前面初始化为空列表结束。
由于这是一门课程,我有一些要求,即我不能在 Python 中使用 csv reader,必须“长途跋涉”。我还需要确保当我将列表的内容写回文件时,它不是附加而是将整个内容转储回文件(擦除和重写)。
我无法弄清楚这里出了什么问题,因为我已经使用 returns 函数和空列表初始化了列表“颜色”以及其他列表。怎么没有定义?
错误信息
Traceback & Error
Traceback (most recent call last):
File "frontend.py", line 71, in <module>
main()
File "frontend.py", line 12, in main
backend.load_file_data()
File "backend.py", line 81, in load_file_data
add_record(colours, colour)
NameError: name 'colours' is not defined
frontend.py
def main():
colours = backend.init_data_structure()
categories = backend.init_data_structure()
prices = backend.init_data_structure()
backend.load_file_data()
backend.py
def init_data_structure():
return []
def add_record(list_name, data):
list_name.append(data)
def load_file_data():
from_file = open("inventory.csv", "r")
line = from_file.readline()
while line != "":
line = line.strip()
fields = line.split(",")
colour = fields[0]
category = int(fields[1])
price = float(fields[2])
add_record(colours, colour)
add_record(categories, category)
add_record(prices, price)
from_file.close()
def save_to_file(list1, list2, list3):
output = ""
l = list_length(list1)
i = 0
to_file = open("inventory.csv", "w")
while i < l:
to_file.write(get_value_at_index(list1, i) + "," + str(get_value_at_index(list2, i)) + "," + str(format(get_value_at_index(list3, i), ".2f")) + "\n")
i += 1
to_file.close()
return output
inventory.csv
Red,1,258.47
Red,1,309.08
Brown,2,456.56
Grey,2,317.43
Yellow,2,355.9
Grey,3,379.39
Green,3,477.49
Rainbox,4,302.05
Grey,4,296.31
Rainbox,2,234.33
Dark Green,2,216.11
Dark Blue,2,266.98
Blue,2,433.44
Purple,1,382.75
Purple,1,154.9
Blue,2,201.95
Purple,4,410.38
Rainbox,4,301.7
Rainbox,4,146.83
Grey,2,248.78
Yellow,3,252.34
Burnt Orange,3,209.41
Rainbox,3,259.92
Cream,3,388.13
Orange,4,251.14
Blue,1,194.5
Blue,1,270.09
Yellow,3,308.93
Blue,3,110.78
Cream,3,187.75
Burnt Orange,3,356.41
Grey,3,133.26
Cream,3,238.03
Grey,2,356.02
Burnt Orange,2,71.43
Purple,1,121
Dark Blue,3,320
Cream,4,105.62
Blue,4,279.6
Cream,4,116.13
Rainbox,4,197.68
Grey,4,329.24
Grey,4,264.09
Grey,4,91.99
Purple,4,343.96
Brown,2,353.41
Yellow,3,357.22
Orange,2,155.29
Dark Blue,3,147.6
我相信我已经在 Zach Young 的帮助下工作了(即颜色在 load_file_data() 函数的范围内不可见。我已经将三个空列表传递给函数作为来自前端的参数并将函数重写如下。
def load_file_data(list1, list2, list3):
from_file = open("inventory.csv", "r")
line = from_file.readline()
while line != "":
line = line.strip()
fields = line.split(",")
column1 = fields[0]
column2 = int(fields[1])
column3 = float(fields[2])
add_record(list1, column1)
add_record(list2, column2)
add_record(list3, column3)
line = from_file.readline()
from_file.close()
问题 我需要构建一个后端函数来读取 csv 文件中的数据,拆分值并将它们附加到多个列表中。列表中 adding/removing/displaying 值的程序正在运行,但现在我需要添加文件 I/O.
我有文件 inventory.csv 并且有一个函数 load_file_data() 用于读取、拆分然后将数据插入到三个不同的列表中,这些列表通过前面初始化为空列表结束。
由于这是一门课程,我有一些要求,即我不能在 Python 中使用 csv reader,必须“长途跋涉”。我还需要确保当我将列表的内容写回文件时,它不是附加而是将整个内容转储回文件(擦除和重写)。
我无法弄清楚这里出了什么问题,因为我已经使用 returns 函数和空列表初始化了列表“颜色”以及其他列表。怎么没有定义?
错误信息
Traceback & Error
Traceback (most recent call last):
File "frontend.py", line 71, in <module>
main()
File "frontend.py", line 12, in main
backend.load_file_data()
File "backend.py", line 81, in load_file_data
add_record(colours, colour)
NameError: name 'colours' is not defined
frontend.py
def main():
colours = backend.init_data_structure()
categories = backend.init_data_structure()
prices = backend.init_data_structure()
backend.load_file_data()
backend.py
def init_data_structure():
return []
def add_record(list_name, data):
list_name.append(data)
def load_file_data():
from_file = open("inventory.csv", "r")
line = from_file.readline()
while line != "":
line = line.strip()
fields = line.split(",")
colour = fields[0]
category = int(fields[1])
price = float(fields[2])
add_record(colours, colour)
add_record(categories, category)
add_record(prices, price)
from_file.close()
def save_to_file(list1, list2, list3):
output = ""
l = list_length(list1)
i = 0
to_file = open("inventory.csv", "w")
while i < l:
to_file.write(get_value_at_index(list1, i) + "," + str(get_value_at_index(list2, i)) + "," + str(format(get_value_at_index(list3, i), ".2f")) + "\n")
i += 1
to_file.close()
return output
inventory.csv
Red,1,258.47
Red,1,309.08
Brown,2,456.56
Grey,2,317.43
Yellow,2,355.9
Grey,3,379.39
Green,3,477.49
Rainbox,4,302.05
Grey,4,296.31
Rainbox,2,234.33
Dark Green,2,216.11
Dark Blue,2,266.98
Blue,2,433.44
Purple,1,382.75
Purple,1,154.9
Blue,2,201.95
Purple,4,410.38
Rainbox,4,301.7
Rainbox,4,146.83
Grey,2,248.78
Yellow,3,252.34
Burnt Orange,3,209.41
Rainbox,3,259.92
Cream,3,388.13
Orange,4,251.14
Blue,1,194.5
Blue,1,270.09
Yellow,3,308.93
Blue,3,110.78
Cream,3,187.75
Burnt Orange,3,356.41
Grey,3,133.26
Cream,3,238.03
Grey,2,356.02
Burnt Orange,2,71.43
Purple,1,121
Dark Blue,3,320
Cream,4,105.62
Blue,4,279.6
Cream,4,116.13
Rainbox,4,197.68
Grey,4,329.24
Grey,4,264.09
Grey,4,91.99
Purple,4,343.96
Brown,2,353.41
Yellow,3,357.22
Orange,2,155.29
Dark Blue,3,147.6
我相信我已经在 Zach Young 的帮助下工作了(即颜色在 load_file_data() 函数的范围内不可见。我已经将三个空列表传递给函数作为来自前端的参数并将函数重写如下。
def load_file_data(list1, list2, list3):
from_file = open("inventory.csv", "r")
line = from_file.readline()
while line != "":
line = line.strip()
fields = line.split(",")
column1 = fields[0]
column2 = int(fields[1])
column3 = float(fields[2])
add_record(list1, column1)
add_record(list2, column2)
add_record(list3, column3)
line = from_file.readline()
from_file.close()