您的位置:首页 > 编程语言 > Python开发

python 读取csv文档hex字符串 转为int并存储

2016-08-01 09:01 309 查看
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 01 08:20:01 2016

@author: user
"""
import csv
import os

# wb中的w表示写入模式,b是文件模式
# 写入一行用writerow
# 多行用writerows

def CSVdatachange(filePathIn,filePathOut):
ContentData = []

#====读取csv文件
csvfile = file(filePathIn, 'rb')
reader = csv.reader(csvfile)
for line in reader:
# print line
newRow=[]
for var in range(len(line)-1):
# print var
newdat=line[var]+line[var+1]
val=int(newdat.upper(),16)
newRow.append(val)
# print newRow
ContentData.append(newRow)
csvfile.close()
#print ContentData
#
#
##====write new data lines to csv
csvfile = file(filePathOut, 'wb')
writer = csv.writer(csvfile)
writer.writerows(ContentData)
csvfile.close()

def GetFileNameAndExt(filename):
(filepath,tempfilename) = os.path.split(filename);
(shotname,extension) = os.path.splitext(tempfilename);
return shotname

fileList=[] #待处理文件路径
fileOutList=[] #输出文件路径
for filename in os.listdir(r'D:\output'):
pa='D:\output\%s'%filename
fileList.append(pa)
name=GetFileNameAndExt(pa)
name+='-short.csv'
pa='D:\shortout\%s'%name
fileOutList.append(pa)

for files in range(0,len(fileList)):
CSVdatachange(fileList[files],fileOutList[files])

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python