5

処理作業の一部を R から Python に移行しようとしています。R では、read.table() を使用して非常に乱雑な CSV ファイルを読み取り、レコードを正しい形式で自動的に分割します。例えば

391788,"HP Deskjet 3050 scanner always seems to break","<p>I'm running a Windows 7 64 blah blah blah........ake this work permanently?</p>

<p>Update: It might have something to do with my computer. It seems to work much better on another computer, windows 7 laptop. Not sure exactly what the deal is, but I'm still looking into it...</p>
","windows-7 printer hp"

は正しく 4 つの列に分割されています。1 つのレコードを複数の行に分割することができ、あちこちにカンマがあります。RI では次のようにします。

read.table(infile, header = FALSE, nrows=chunksize, sep=",", stringsAsFactors=FALSE)

これを同様にうまく行うことができるPythonの何かがありますか?

ありがとう!

4

2 に答える 2

3

このpandasモジュールは、 を含む多くの R に似た関数とデータ構造も提供しますread_csv。ここでの利点は、データがDataFramepandas として読み込まれることです。これは、標準の python リストまたは dict よりも操作が少し簡単です (特に R に慣れている場合)。次に例を示します。

>>> from pandas import read_csv
>>> ugly = read_csv("ugly.csv",header=None)
>>> ugly
        0                                              1  \
0  391788  HP Deskjet 3050 scanner always seems to break   

                                                   2                     3  
0  <p>I'm running a Windows 7 64 blah blah blah.....  windows-7 printer hp  
于 2013-10-23T14:25:30.707 に答える