日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python处理csv文件列错位_CSV文件分割与列异常处理的python脚本

發布時間:2023/12/2 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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脚本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。