请教PYTHON读取CSV文件方法
答案:2 悬赏:70
解决时间 2021-12-23 01:49
- 提问者网友:宿醉何为情
- 2021-12-22 15:40
请教PYTHON读取CSV文件方法
最佳答案
- 二级知识专家网友:说多了都是废话
- 2021-12-22 16:04
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from Log import Loginfo
import cgi, os, csv, sys, re
reload(sys)
sys.setdefaultencoding('utf8')
print "Content-Type: text/html charset=utf-8 \n"
fileitem = ''
def get_cgi_file():
''''''
global fileitem,device_id,maxDeviceID,maxDriverID,channelid,ChannelDeviceType
form = cgi.FieldStorage()
# 获取文件名
fileitem = form['filename1']
# 检测文件是否上传
if fileitem.filename:
# 去掉文件路径,获取文件名称
fn = os.path.basename(fileitem.filename)
open(global_var.uploadfile_path, 'wb').write(fileitem.file.read())
#message = '文件"' + fn + '" 上传成功!'
#print message
else:
message = '没有文件上传!'
print message
def convert_gbk2utf8():
data_list = []
fd = open(global_var.uploadfile_path, 'rb')
csvfd = csv.reader(fd)
for c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14 in csvfd:
c1_u = c1.decode('gb2312').encode('utf-8')
c2_u = c2.decode('gb2312').encode('utf-8')
c3_u = c3.decode('gb2312').encode('utf-8')
c4_u = c4.decode('gb2312').encode('utf-8')
c4_u = c4.decode('gb2312').encode('utf-8')
c5_u = c5.decode('gb2312').encode('utf-8')
c6_u = c6.decode('gb2312').encode('utf-8')
c7_u = c7.decode('gb2312').encode('utf-8')
c8_u = c8.decode('gb2312').encode('utf-8')
c9_u = c9.decode('gb2312').encode('utf-8')
c10_u = c10.decode('gb2312').encode('utf-8')
c11_u = c11.decode('gb2312').encode('utf-8')
c12_u = c12.decode('gb2312').encode('utf-8')
c13_u = c13.decode('gb2312').encode('utf-8')
c14_u = c14.decode('gb2312').encode('utf-8')
data_row_list = [c1_u,c2_u,c3_u,c4_u,c5_u,c6_u,c7_u,c8_u,c9_u,c10_u,c11_u,c12_u,c13_u,c14_u]
data_list.append(data_row_list)
fd.close()
#log.write_debug(data_list)
return data_list
def analy_csv_file(data_list):
for rownum in range(len(data_list)):
if rownum == 0:
attrib = data_list[rownum]
else:
for i in range(len(attrib)):
#这里循环取数据,依据是列名
if attrib[i] == '你的列名':
print data_list[rownum][i]
if __name__ == '__main__':
log = Loginfo.Loginfo()
get_cgi_file()
try:
data_list = convert_gbk2utf8()
except Exception as e:
print("正在导入的表格列数不对,请检查!")
deleteDevice()删了一些函数,这样应该可以看得懂吧,c14_u是列,有多少列就多少个,这是转换编码。analy_csv_file(data_list)里面对拿到的文件做处理
# -*- coding: UTF-8 -*-
from Log import Loginfo
import cgi, os, csv, sys, re
reload(sys)
sys.setdefaultencoding('utf8')
print "Content-Type: text/html charset=utf-8 \n"
fileitem = ''
def get_cgi_file():
''''''
global fileitem,device_id,maxDeviceID,maxDriverID,channelid,ChannelDeviceType
form = cgi.FieldStorage()
# 获取文件名
fileitem = form['filename1']
# 检测文件是否上传
if fileitem.filename:
# 去掉文件路径,获取文件名称
fn = os.path.basename(fileitem.filename)
open(global_var.uploadfile_path, 'wb').write(fileitem.file.read())
#message = '文件"' + fn + '" 上传成功!'
#print message
else:
message = '没有文件上传!'
print message
def convert_gbk2utf8():
data_list = []
fd = open(global_var.uploadfile_path, 'rb')
csvfd = csv.reader(fd)
for c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14 in csvfd:
c1_u = c1.decode('gb2312').encode('utf-8')
c2_u = c2.decode('gb2312').encode('utf-8')
c3_u = c3.decode('gb2312').encode('utf-8')
c4_u = c4.decode('gb2312').encode('utf-8')
c4_u = c4.decode('gb2312').encode('utf-8')
c5_u = c5.decode('gb2312').encode('utf-8')
c6_u = c6.decode('gb2312').encode('utf-8')
c7_u = c7.decode('gb2312').encode('utf-8')
c8_u = c8.decode('gb2312').encode('utf-8')
c9_u = c9.decode('gb2312').encode('utf-8')
c10_u = c10.decode('gb2312').encode('utf-8')
c11_u = c11.decode('gb2312').encode('utf-8')
c12_u = c12.decode('gb2312').encode('utf-8')
c13_u = c13.decode('gb2312').encode('utf-8')
c14_u = c14.decode('gb2312').encode('utf-8')
data_row_list = [c1_u,c2_u,c3_u,c4_u,c5_u,c6_u,c7_u,c8_u,c9_u,c10_u,c11_u,c12_u,c13_u,c14_u]
data_list.append(data_row_list)
fd.close()
#log.write_debug(data_list)
return data_list
def analy_csv_file(data_list):
for rownum in range(len(data_list)):
if rownum == 0:
attrib = data_list[rownum]
else:
for i in range(len(attrib)):
#这里循环取数据,依据是列名
if attrib[i] == '你的列名':
print data_list[rownum][i]
if __name__ == '__main__':
log = Loginfo.Loginfo()
get_cgi_file()
try:
data_list = convert_gbk2utf8()
except Exception as e:
print("正在导入的表格列数不对,请检查!")
deleteDevice()删了一些函数,这样应该可以看得懂吧,c14_u是列,有多少列就多少个,这是转换编码。analy_csv_file(data_list)里面对拿到的文件做处理
全部回答
- 1楼网友:兮沫♡晨曦
- 2021-12-22 17:09
这两天刚好看到,python cookbook上有说到。这里是三种读取csv的方法。
文件格式是这样的
region,date_,raw_acu
zh_ch,jan 27 2017,208172
import csv
from collections import namedtuple
# with open('data.csv') as f:
# f_csv = csv.reader(f)
# headers = next(f_csv)
# for row in f_csv:
# # print(row)
# print(row[0], row[1])
# with open('data.csv', encoding='utf-8-sig') as f:
# f_csv = csv.reader(f)
# headers = next(f_csv)
# print(headers)
# row = namedtuple('row', headers)
# for r in f_csv:
# row = row(*r)
# print(row.region, row.date_)
with open('data.csv', encoding='utf-8-sig') as f:
f_csv = csv.dictreader(f)
for row in f_csv:
print(row['date_'], row)
具体可以看这个文档。http://python3-cookbook.readthedocs.io/zh_cn/latest/c06/p01_read_write_csv_data.html。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯