当前位置:主页 > 软件编程 > Python代码 >

python3读取csv和xlsx文件的实例

时间:2021-03-06 10:11:15 | 栏目:Python代码 | 点击:

基于win10系统,python3.6

读取csv

使用csv函数包,安装 pip install csv

使用方法:

import csv
def fileload(filename = '待读取.csv'):
  csvfile = open(filename, encoding = 'utf-8')
  data = csv.reader(csvfile)
  dataset = []
  for line in data:
    dataset.append(line)
  csvfile.close()
  return dataset

读取xlsx

使用xlrd函数包,安装: pip install xlrd

使用方法:

import xlrd
def fileload(filename = '待读取.xlsx'):
  dataset = []
  workbook = xlrd.open_workbook(filename)
  table = workbook.sheets()[0]
  for row in range(table.nrows):
    dataset.append(table.row_values(row))
  return dataset

这两个较为常用,需要知道如何使用。

您可能感兴趣的文章:

相关文章