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])
"""
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])
相关文章推荐
- IP字符串转为int类型存储代码
- Python读取mat文件,并转为csv文件的实例
- [Python]网络数据采集概述(2)—存储数据及读取文档
- python数据存储系列教程——python对象与json字符串的相互转化,json文件的存储与读取
- python读取csv文档
- python---函数(返回整型最大/最小值、返回字符串最长的串、函数返回任意的module的帮助文档内容、读取目录下内容、)
- python 循环读取txt文档 并转换成csv的方法
- python 结巴分词中 按行读取 csv中的文件 并将分词存储到csv中
- Python读取mat文件,并转为csv文件
- python 循环读取txt文档 并转换成csv
- 三、Python:函数、模块、文档字符串
- python中的文档字符串(docString)
- python读写文件,分析字符串及读取目录下文件名的实践
- C# int 转 16进制的字符串 in to hex string
- 用oracle的java存储过程实现BLOB字段的字符串读取
- 利用Python将word 2007的文档转为pdf文件
- python中的文档字符串(docString)
- MFC 用串行化实现文档存储和读取功能
- Python-读取csv文件
- python中的文档字符串(docString)