2016-08-02 4 views
3

パンダにデータセットをロードしようとしていて、ステップ1を過ぎたように見えることはありません。これは明らかです。回答。データは主に漢字であり、これが問題になる可能性があります。パンダがデータを読み込むことができない、CSVエンコーディングミステリー

.CSVは非常に大きく、ここで見つけることができます:私は以下の私のコードでは、週1

にしようとしていますhttp://weiboscope.jmsc.hku.hk/datazip/ 、私が何を参照しようとする試みを含め、私が試みデコードの3つのタイプを識別しますエンコードが使用されました

import pandas 
import chardet 
import os 


#this is what I tried to start 
    data = pandas.read_csv('week1.csv', encoding="utf-8") 

    #spits out error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9a in position 69: invalid start byte 

#Code to check encoding -- this spits out ascii 
bytes = min(32, os.path.getsize('week1.csv')) 
raw = open('week1.csv', 'rb').read(bytes) 
chardet.detect(raw) 

#so i tried this! it also fails, which isn't that surprising since i don't know how you'd do chinese chars in ascii anyway 
data = pandas.read_csv('week1.csv', encoding="ascii") 

#spits out error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128) 

#for god knows what reason this allows me to load data into pandas, but definitely not correct encoding because when I print out first 5 lines its gibberish instead of Chinese chars 
data = pandas.read_csv('week1.csv', encoding="latin1") 

ご協力いただければ幸いです!

EDIT:これはオープンWeiboscopeであることを

import csv 
import pandas as pd 

def clean_weiboscope(file, nrows=0): 
    res = [] 
    with open(file, 'r', encoding='utf-8', errors='ignore') as f: 
     reader = csv.reader(f) 
     for i, row in enumerate(f): 
      row = row.replace('\n', '') 
      if nrows > 0 and i > nrows: 
       break 
      if i == 0: 
       headers = row.split(',') 
      else: 
       res.append(tuple(row.split(','))) 
    df = pd.DataFrame(res) 
    return df 

my_df = clean_weiboscope('week1.csv', nrows=0) 

は、私も将来サーチャーのために追加したい:@Kristofが提供する答えは、プログラムがそうであるように私の同僚は、昨日一緒に入れ、実際の作業ではありません2012年のデータです。

+0

UTF-8に見えますが、いくつかのダフ文字がデコードを失敗し、テキスト欄にありますあなたができることの1つは、それらの行をスキップすることですが、これは一度に各行を試す必要があります。私はhttps://docs.python.org/2/library/codecs.htmlに記載されているすべての中国語のエンコードを試しましたが、それらはすべて異なる理由で失敗しました。テキストには、ある種の絵文字やバイトデータが含まれている可能性があります。 – EdChum

+0

テキスト列のパーサとして美しいスープを使用するとうまくいくかもしれません:http://stackoverflow.com/questions/30729633/some-utf-8-codec-cant-decode-byte bs4を使用するfuncを定義しますこれをパラメータとして渡す 'pd.read_csv(....、converters = {'text'}:your_func})' – EdChum

答えて

1

入力ファイルに問題があるようです。全体にエンコードエラーがあります。

CSVファイルをバイナリとして読み取り、バイナリ文字列をデコードして間違った文字を置き換えることです。

例(チャンク-、コード読み取り用source):、それは正しいエンコーディングだよう

in_filename = 'week1.csv' 
out_filename = 'repaired.csv' 

from functools import partial 
chunksize = 100*1024*1024 # read 100MB at a time 

# Decode with UTF-8 and replace errors with "?" 
with open(in_filename, 'rb') as in_file: 
    with open(out_filename, 'w') as out_file: 
     for byte_fragment in iter(partial(in_file.read, chunksize), b''): 
      out_file.write(byte_fragment.decode(encoding='utf_8', errors='replace')) 

# Now read the repaired file into a dataframe 
import pandas as pd 
df = pd.read_csv(out_filename) 

df.shape 
>> (4790108, 11) 

df.head() 

sample output

関連する問題