处理excel
import xlrd
# 通过网络获取的文件,首先暂存本地
excel = request.FILES.get('dev_excel')
path_url = os.path.join(CFG.BASE_DIR, CFG.EXCEL_PATH,'append_dev_excel.xlsx')
with open(path_url, 'wb') as f:
for chunk in excel.chunks():
f.write(chunk)
# 本地文件直接从这一步开始
excel_data = xlrd.open_workbook(path_url)
sheet = excel_data.sheet_by_index(0) # 第一个工作sheet
for i in range(1, int(sheet.nrows)):
# 遍历每一行数据
print(sheet.row_values(i)[1]) # 坐标(i,1)的值
pass
# 有时会报错格式不支持,可能是xlrd的版本不匹配
处理csv
# csv 以逗号分隔,读取文件流以 逗号和回车分割行和列
table= request.FILES.get('dev_csv')
# print(str(table))
extension = str(table).split(".")[1].lower()
if extension == 'csv':
# 确认为csv文件
# 读出数据
read_csv(table)
def read_csv(table):
detail = table.read().decode("utf-8")
lines = detail.split('n') # 回车分割行
return [line.split(',')for line in lines[1:]] # 逗号分割列



