python处理csv文件列错位_CSV文件分割与列异常处理的python脚本
#coding=utf-8
‘‘‘腳本說明
用來解決csv文件的列異常問題(以逗號分隔符為例):
csv文件有些列含有換行符、逗號等特殊符號,這就導致csv文件出現列異常的情況。
此腳本將csv文件輸出成如下文件:源文件名-正確列-文件序號.csv 源文件名-錯誤列.csv 源文件名-info.txt
常用文本編碼: gbk,gb2312,utf-8,utf-8_sig
★:因為沒有實現文本編碼轉換,所以“輸入文件編碼”和“輸出文件編碼”要寫成一致才行,其中輸入文件編碼必須正確
不知道為什么有些gbk輸入文件會報錯:
for count,line in enumerate(open(fileName+fileExt,‘rU‘,encoding=inFileEncoding)):
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf8 in position 5902: illegal multibyte sequence
解決辦法:
使用powershell轉換格式成utf-8,然后在使用本腳本來處理。其中powershell轉換格式的代碼如下:
Get-Content -path .\2014.csv|Out-File .\2014-1.csv -Encoding "utf-8"‘‘‘
#單行注釋
‘‘‘多行注釋:
取模運算:
if a % b == 0 :
print ‘yes‘
else:
print ‘no‘
整除語法: 2//1‘‘‘
importos,csv,sys
fileName=‘2013-1‘fileExt=‘.csv‘
#輸入文件編碼
inFileEncoding=‘utf-8‘
#輸出文件編碼
outFileEncoding=‘utf-8‘
#行號
rowscount=1
#分隔符號
delimiterChar=‘,‘rowLength=0#分割后文件名開始編號
filecount=0#計算文件總行數
count=-1
for count,line in enumerate(open(fileName+fileExt,‘rU‘,encoding=inFileEncoding)):passcount+= 1
#源文件名-info.txt
filename3=fileName+‘-info‘+‘.txt‘file3=open(filename3,‘w+‘,encoding=‘utf-8‘)
statinfo=os.stat(fileName+fileExt)print("\n文件名稱:"+ fileName+fileExt)print("\n文件名稱:"+ fileName+fileExt,file=file3 )print("文件行數:"+str(count) )print("文件行數:"+ str(count),file=file3 )print("文件大小:"+ str(statinfo.st_size//1024//1024)+"M")print("文件大小:"+ str(statinfo.st_size//1024//1024)+"M",file=file3)
strContentInput=input("請輸入要分割成的文件數:")ifstrContentInput.isdigit():
contentInput=int(strContentInput)else:print("★ : 您輸入的"+strContentInput+"不是數字 !",file=sys.stderr)print("★ : 您輸入的"+strContentInput+"不是數字 !",file=file3)#退出腳本
sys.exit()print("分割成的文件數:"+strContentInput)print("分割成的文件數:"+strContentInput,file=file3)
splitLineCount=count//contentInputprint("每文件行數:"+str(splitLineCount))print("每文件行數:"+str(splitLineCount),file=file3)#可以用一個list包含文件對象列表#源文件名-正確列-文件序號.csv
fileList=[]
fileIndex=0print("分割后的文件名:")print("分割后的文件名:",file=file3)while fileIndex
filetmpname=fileName+‘-正確列-‘+str(fileIndex)+fileExtprint(" "+filetmpname)print(" "+filetmpname,file=file3)
filetmp=open(filetmpname,‘w+‘,encoding=outFileEncoding)
fileList.append(filetmp)
fileIndex=fileIndex+1
#源文件名-錯誤列.csv
filename2=fileName+‘-錯誤列‘+fileExt
file2=open(filename2,‘w+‘,encoding=outFileEncoding)print("分割后的文件名(列異常數據):")print("分割后的文件名(列異常數據):",file=file3)print(" "+filename2)print(" "+filename2,file=file3)
with open(fileName+‘.csv‘,newline=‘‘,encoding=inFileEncoding) as csvfile:
spamreader=csv.reader(csvfile,delimiter=delimiterChar)for line inspamreader:#列數為0時,讀取第一行作為準確的列數。
if ( rowLength ==0 ):
rowLength=len(line)#列數不為0時,當前行的列數與其匹配,將匹配的和不匹配的保存到不同的文件。
else:if ( rowLength ==len(line) ):#輸出到對應文件序號的文件中: 行數“整除”分割行數
if ( rowscount//splitLineCount > len(fileList)-1):print((‘,‘.join(line)),file=fileList[len(fileList)-1])else:print((‘,‘.join(line)),file=fileList[rowscount//splitLineCount])else:print((‘,‘.join(line)),file=file2)
rowscount=rowscount+1
#關閉文件
file2.close()delfile2
file3.close()delfile3
fileIndex=0while fileIndex
fileList[fileIndex].close()
fileIndex=fileIndex+1
#刪除整個fileList
del fileList
總結
以上是生活随笔為你收集整理的python处理csv文件列错位_CSV文件分割与列异常处理的python脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android sdk build-to
- 下一篇: 备案项目简介怎么写(备案项目简介)