私はCSVファイルを読んでいると困っています。csvから余分な引用符を削除するには?
私はreplaceメソッドを試しました。しかしnumpyはそれをサポートしていません。
csvファイル形式は次のとおりです。
"num","phone","sensorID","press","temp","accel","gps_lat","gps_lng","time"
"1","null","A0:E6:F8:7B:16:EA","0","17","1.25","0","0","2016-12-14 13:34:59"
"2","null","A0:E6:F8:7B:16:A9","0","18","1.19","0","0","2016-12-14 13:34:59"
"3","null","A0:E6:F8:7B:15:A5","0","18","1.19","0","0","2016-12-14 13:34:59"
"4","null","A0:E6:F8:7B:16:EA","0","17","1.25","0","0","2016-12-14 13:35:00"
"5","null","A0:E6:F8:7B:16:A9","0","18","1.19","0","0","2016-12-14 13:35:00"
"6","null","A0:E6:F8:7B:15:A5","0","19","1.38","0","0","2016-12-14 13:35:00"
"7","null","A0:E6:F8:7B:16:D6","0","18","1.12","0","0","2016-12-14 13:35:01"
"8","null","A0:E6:F8:7B:16:EA","0","17","1.31","0","0","2016-12-14 13:35:01"
"9","null","A0:E6:F8:7B:15:A5","0","19","1.38","0","0","2016-12-14 13:35:01"
しかし、私はnumpy.loadtxtでこのファイルを使用する場合、結果はちょうど私が」を削除したいこの
ソースコード
import numpy as np
a= np.loadtxt('db_file.csv', delimiter=',', dtype='str', unpack=True)
print a
結果
[['"num"' '"1"' '"2"' ..., '"6979"' '"6980"' '"6981"']
['"phone"' '"null"' '"null"' ..., '" 821099631345"' '" 821099631345"'
'" 821099631345"']
['"sensorID"' '"A0:E6:F8:7B:16:EA"' '"A0:E6:F8:7B:16:A9"' ...,
'"A0:E6:F8:7B:16:EA"' '"A0:E6:F8:7B:16:A9"' '"A0:E6:F8:7B:16:D6"']
...,
['"gps_lat"' '"0"' '"0"' ..., '37.596332"' '"37.596332"' '"37.596332"']
['"gps_lng"' '"0"' '"0"' ..., '"127.031773"' '"127.031773"' '"127.031773"']
['"time"' '"2016-12-14 13:34:59"' '"2016-12-14 13:34:59"' ...,
'"2016-12-15 00:03:11"' '"2016-12-15 00:03:11"' '"2016-12-15 00:03:12"']]
のようなものですこの1つ。
だから本当にこのリストが欲しい。
[['num', '1', '2' ..., '6979', '6980', '6981']
['phone', 'null', 'null' ..., '821099631345', ' 821099631345'
' 821099631345']
['sensorID', 'A0:E6:F8:7B:16:EA', 'A0:E6:F8:7B:16:A9' ...,
'A0:E6:F8:7B:16:EA', 'A0:E6:F8:7B:16:A9', 'A0:E6:F8:7B:16:D6']
...,
['gps_lat', '0', '0' ..., '37.596332' '37.596332' '37.596332']
['gps_lng' '0' '0' ..., '127.031773' '127.031773' '127.031773']
['time' '2016-12-14 13:34:59' '2016-12-14 13:34:59' ...,
'2016-12-15 00:03:11' '2016-12-15 00:03:11' '2016-12-15 00:03:12']]
私はどのようなコードを使用しますか?
件名は修正が必要です。 – hpaulj
'pd.read_csv'はこのファイルを問題なく処理しているようです。 'genfromtxt'も動作させることができますが、' pandas'があれば簡単になります。 – hpaulj
ここに便利なものがありますか? http://stackoverflow.com/questions/2664790/reading-csv-files-in-numpy-where-delimiter-is –