最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用python讀取CSV文件時(shí)遇到編碼問題解決方案

 更新時(shí)間:2023年08月14日 08:32:39   作者:myrj  
這篇文章主要介紹了用python讀取CSV文件時(shí)遇到編碼問題,本文給大家分享最優(yōu)解決方案,通過使用csvkit,它使用自動(dòng)檢測(cè)適當(dāng)?shù)木幋a和解碼,需要的朋友可以參考下

嘗試使用python讀取CSV文件時(shí)遇到障礙。

更新:如果只想跳過字符或錯(cuò)誤,可以打開文件,如下所示:

with open(os.path.join(directory, file), 'r', encoding="utf-8", errors="ignore") as data_file:

到目前為止,我已經(jīng)嘗試過了。

for directory, subdirectories, files in os.walk(root_dir):

for file in files:

with open(os.path.join(directory, file), 'r') as data_file:

reader = csv.reader(data_file)

for row in reader:

print (row)

我得到的錯(cuò)誤是:

UnicodeEncodeError: 'charmap' codec can't encode characters in position 224-225: character maps to

我試過了

with open(os.path.join(directory, file), 'r', encoding="UTF-8") as data_file:

錯(cuò)誤:

UnicodeEncodeError: 'charmap' codec can't encode character '\u2026' in position 223: character maps to

現(xiàn)在,如果我只打印data_file,它說它們是cp1252編碼的,但是如果我嘗試

with open(os.path.join(directory, file), 'r', encoding="cp1252") as data_file:

我得到的錯(cuò)誤是:

UnicodeEncodeError: 'charmap' codec can't encode characters in position 224-225: character maps to

我也嘗試了推薦的套餐。

我得到的錯(cuò)誤是:

UnicodeEncodeError: 'charmap' codec can't encode characters in position 224-225: character maps to

我要解析的行是:

2015-11-28 22:23:58,670805374291832832,479174464,"MarkCrawford15","RT @WhatTheFFacts: The tallest man in the world was Robert Pershing Wadlow of Alton, Illinois. He was slighty over 8 feet 11 inches tall.","None

任何想法或幫助表示贊賞。

解決方案

我將使用csvkit,它使用自動(dòng)檢測(cè)適當(dāng)?shù)木幋a和解碼。例如

import csvkit
reader = csvkit.reader(data_file)

正如聊天解決方案所述,

for directory, subdirectories, files in os.walk(root_dir):
for file in files:
with open(os.path.join(directory, file), 'r', encoding="utf-8") as data_file:
reader = csv.reader(data_file)
for row in reader:
data = [i.encode('ascii', 'ignore').decode('ascii') for i in row]
print (data)

到此這篇關(guān)于用python讀取CSV文件時(shí)遇到編碼問題的文章就介紹到這了,更多相關(guān)python讀取CSV文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

崇明县| 兴业县| 伊通| 乐至县| 沂源县| 昌邑市| 高陵县| 平度市| 寿阳县| 宣城市| 古丈县| 吴堡县| 含山县| 即墨市| 黑龙江省| 澄江县| 阿荣旗| 闻喜县| 夹江县| 湛江市| 上林县| 沾化县| 武城县| 丹棱县| 碌曲县| 大埔县| 汝南县| 新津县| 丹阳市| 尼木县| 石林| 合阳县| 铜川市| 莆田市| 阳高县| 留坝县| 如东县| 阜康市| 霍州市| 黔江区| 治多县|