[英]How do I import from xlsx file via Openpyxl into a Python Class?
I have an xlsx file which opens with Openpyxl.我有一个用 Openpyxl 打开的 xlsx 文件。 Each row contains student_id, first_name, second_name, parent_name and parent_email.
每行包含 student_id、first_name、second_name、parent_name 和 parent_email。
I have created a method called Students我创建了一个名为学生的方法
I want the program to take each row and assign it to the method eg.我希望程序获取每一行并将其分配给方法,例如。
Students(student_id = ['A -row number'], first_name = ['B -row number'], second_name = ['C - row number] ...etc
so that it cycles through all of my students and automatically adds them以便它循环遍历我所有的学生并自动添加他们
pretty sure the answer lies in a loop such as:很确定答案在一个循环中,例如:
for row in ws.rows:
This way was much better.这种方式要好得多。 Ditch Openpyxl, open it from a csv file.
放弃 Openpyxl,从 csv 文件打开它。 In the csv file I have rows each individual bit of information in columns A,B,C,D,E
在 csv 文件中,我在 A、B、C、D、E 列中有每一行信息
eg.例如。 A1 = Potter, Harry B1 = Potter C1 = Harry... etc
A1 = 波特、哈利 B1 = 波特 C1 = 哈利……等等
import csv
#sorts all the student data into a class
class Students():
def __init__(self,student_id, first_name,second_name,student_parent,parent_email):
self.student_id = student_id
self.first_name = first_name
self.second_name = second_name
self.student_parent = student_parent
self.parent_email = parent_email
def __repr__(self):
return self.student_id + " " + self.first_name + " " +self.second_name + " " + self.student_parent + " " + self.parent_email
student_list = []
#opens the csv file, takes out the information and saves them in the Student Class
student_file = open('student_list.csv')
student_file_reader = csv.reader(student_file)
for row in student_file:
row_string = str(row)
row_parts1,row_parts2,row_parts3,row_parts4,row_parts5 = row_string.split(',')
student_list.append(Students(row_parts1,row_parts2,row_parts3,row_parts4,row_parts5))
...and there you have it, all in formation in objects within a list perfect. ...你有它,所有在一个列表中的对象中形成完美的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.