简体   繁体   English

如何通过 Openpyxl 从 xlsx 文件导入 Python 类?

[英]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.

相关问题 如何使用openpyxl从python中的xlsx文件中读取货币符号? - How to read a currency symbol from an xlsx file in python using openpyxl? (Python-openpyxl)无法导入导出的xlsx文件 - (Python - openpyxl) Can't import the exported xlsx file Python:如何从 xlsx 文件中抓取数据的语法? - Python: How do I syntax data scraping from xlsx file? 如何使用Python openpyxl查找每行Excel xlsx文件中的列数? - How do you find the number of columns in an Excel xlsx file per row with Python openpyxl? Python:如何从一个XLSX中搜索一个字符串,使其位于另一个XLSX文件中? - Python: How do I search a string from one XLSX to be in another XLSX file? 如何在 python 上的 openpyxl 的帮助下在现有的 xlsx 文件上写入文本? - How write text on an existing xlsx file with the help of openpyxl on python? 如何使用 openpyxl 从字典创建 Excel 文件扩展名 .xlsx? - How to create an Excel file extension .xlsx from a dictionary using openpyxl? 在python中使用openpyxl模块编写xlsx文件 - write xlsx file using openpyxl module in python 使用 Openpyxl 打开 xlsx 文件会给出 KeyError '$A$' - Python - Opening xlsx file with Openpyxl gives KeyError '$A$' - Python Python如何使用numpy导入xlsx文件 - Python How to Import xlsx file using numpy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM