使用python读取csv并转化成list of list的方法
2018-03-25 11:30
961 查看
很多面试题目都需要处理csv文件,以下是两种处理csv文件的方法:
方法一: 使用Python基础语言编写def read_csv(file_name):
f = open(file_name, 'r')
content = f.read()
final_list = list()
rows = content.split('\n')
for row in rows:
final_list.append(row.split(','))
return final_list方法二: 使用CSV 模块
import csv
f = open(file_name, 'r')
csvreader = csv.reader(f)
final_list = list(csvreader)
方法一: 使用Python基础语言编写def read_csv(file_name):
f = open(file_name, 'r')
content = f.read()
final_list = list()
rows = content.split('\n')
for row in rows:
final_list.append(row.split(','))
return final_list方法二: 使用CSV 模块
import csv
f = open(file_name, 'r')
csvreader = csv.reader(f)
final_list = list(csvreader)
相关文章推荐
- Python pandas读取csv文件直接转化成二维列表(Dataframe转化为list)
- php使用fgetcsv读取csv文件出现乱码的解决方法
- Python操作列表之List.insert()方法的使用
- Python使用cx_Oracle模块将oracle中数据导出到csv文件的方法
- 在Python中操作列表之List.append()方法的使用
- Python使用xlrd读取Excel格式文件的方法
- selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
- Python中List.index()方法的使用教程
- Python中list列表的一些进阶使用方法介绍
- 在Python中操作列表之list.extend()方法的使用
- python之模块csv之 读取CSV文件(reader和DictReader2个方法)
- 使用python读取csv文件,并将数据更新至mysql
- Python操作列表之List.insert()方法的使用
- 在Python中操作列表之List.append()方法的使用
- list集合怎么转化成一个javaBean对象,及常见的使用方法(全)
- Python导出数据到Excel可读取的CSV文件的方法
- 在Python中操作列表之List.pop()方法的使用
- selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
- Python中List.count()方法的使用教程
- php使用fgetcsv读取csv文件出现乱码的解决方法