python - 我想在 Python 中将逗号分隔的文件作为数组读取
问题描述
我有文本文件
01,Jay,Sharma
02,Rushi,Patel
我想将此文本文件作为数组读取并希望输出为
student no : 01
Name : Jay
Surname : Sharma
student no : 02
Name : Rushi
Surname : Patel
实际上我是 Python 的新手,我可以将它作为数组读取,但我想要准确的输出,请任何人帮助我。
class RowReader:
def fileRead(self,filepath):
textfile = open(filepath, 'r')
data = []
for line in textfile:
row_data = line.strip("\n").split(',')
print(row_data)
file = RowReader()
file.fileRead(file_path)
先感谢您
解决方案
有一个库可以在 Python 中读取 csv 文件:csv
. 您可以使用它轻松读取 csv 文件。
import csv
data = []
with open("filename.csv", "r") as f:
reader = csv.reader(f)
for line in reader:
data.append(line)
for d in data:
print("student no : {}\nName : {}\nSurname : {}".format(
d[0], d[1], d[2]))
推荐阅读
- python - 在 python 中使用 sqlalchemy.select() 从 Postgresq 数据库中获取数据
- bash - 设置多个变量以在条件语句中调用
- swift - 如何在 swift 中使用 alamofire 发布字典数组?
- ruby-on-rails - rswag rails gem,无法识别我创建 swagger.yml 的模式
- javascript - 以角度将文件附加到FormData后无法上传文件
- javascript - FullCalendar v4.2.0 destroy() 不工作
- swift - 从 Swift 中的格式化字符串中提取货币值
- python-3.x - 如何删除熊猫数据框中的日期和离开时间?
- python - 需要一个 python for 循环的替代方案,它每 5 分钟迭代一定数量的文件并执行一些操作
- spring - 在 SpringBoot 中将参数从过滤器传递到业务服务