我有一個 CSV,它的前幾行是這樣的:
3Blue1Brown;UCYO_jab_esuFRV4b17AJtAw;Q&A #2 Net Neutrality Nuance;liL66CApESk;2017-12-14 03:59:29 00:00;141644;4661;107;329;1409.0;43.5607476635514;0.002322724577108808;100.52803406671399
3Blue1Brown;UCYO_jab_esuFRV4b17AJtAw;The hardest problem on the hardest test;OkmNXy7er84;2017-12-08 04:52:24 00:00;13109536;346554;5569;19721;1415.0;62.229125516250676;0.0015043247907477427;9264.689752650176
當我嘗試將它加載到我的 NumPy 陣列中時,我收到了一些我不明白的錯誤。我想這可能與特殊字符有關?或者可能是這個 CSV 資料的編碼格式?這是代碼:
from numpy import loadtxt
import numpy as np
datas_path = 'target/youtube_videos.csv'
data = np.genfromtxt(datas_path, delimiter=';', dtype=None, names=True,\
deletechars="~!@#$%^&*()-= ~\|]}[{';: /?.>,<.", case_sensitive=True)
這是錯誤:
Fail to execute line 11: deletechars="~!@#$%^&*()-= ~\|]}[{';: /?.>,<.", case_sensitive=True)
Traceback (most recent call last):
File "/tmp/1636087867308-0/zeppelin_python.py", line 158, in <module>
exec(code, _zcUserQueryNameSpace)
File "<stdin>", line 11, in <module>
File "/home/joesan/.pyenv/versions/3.7.8/lib/python3.7/site-packages/numpy/lib/npyio.py", line 2124, in genfromtxt
raise ValueError(errmsg)
ValueError: Some errors were detected !
Line #60 (got 3 columns instead of 13)
Line #353 (got 3 columns instead of 13)
Line #720 (got 3 columns instead of 13)
Line #3008 (got 3 columns instead of 13)
Line #3077 (got 3 columns instead of 13)
Line #3129 (got 3 columns instead of 13)
Line #3154 (got 3 columns instead of 13)
Line #3163 (got 3 columns instead of 13)
Line #3175 (got 3 columns instead of 13)
Line #3290 (got 3 columns instead of 13)
Line #3300 (got 3 columns instead of 13)
Line #3310 (got 3 columns instead of 13)
Line #3316 (got 3 columns instead of 13)
Line #3321 (got 3 columns instead of 13)
Line #3328 (got 3 columns instead of 13)
Line #3334 (got 3 columns instead of 13)
Line #3340 (got 3 columns instead of 13)
Line #3361 (got 3 columns instead of 13)
Line #3366 (got 3 columns instead of 13)
Line #3367 (got 3 columns instead of 13)
Line #3375 (got 3 columns instead of 13)
Line #3385 (got 3 columns instead of 13)
Line #3397 (got 3 columns instead of 13)
Line #3407 (got 3 columns instead of 13)
Line #3433 (got 3 columns instead of 13)
Line #3444 (got 3 columns instead of 13)
Line #3450 (got 3 columns instead of 13)
Line #3452 (got 3 columns instead of 13)
Line #3482 (got 3 columns instead of 13)
Line #3511 (got 3 columns instead of 13)
Line #3522 (got 3 columns instead of 13)
Line #3531 (got 3 columns instead of 13)
Line #3536 (got 3 columns instead of 13)
第 60 行是上面給出的 CSV 檔案中的第一條記錄。
編輯:我設法像這樣修復它:
data = np.genfromtxt(datas_path, delimiter=';', dtype=str, comments='%', names=True,\
deletechars="~!@#$%^&*()-= ~\|]}[{';: /?.>,<.", case_sensitive=True)
但現在這條線失敗了:
Adam Savage’s Tested;UCiDJtJKMICpb9B1qf7qjEOA;99% Invisible - The Adam Savage Project - 10/6/20;ClxSdX3ynGQ;2020-10-03 01:45:19 00:00;28334;782;29;87;385.0;26.96551724137931;0.0030705159878591094;73.59480519480519
uj5u.com熱心網友回復:
除非您有使用 Numpy 的特定理由,否則 Pandas 可能是更好的選擇,因為它比 Numpy 更適合決議字串。此代碼段說明了如何應用 Pandas 使用分號作為分隔符/分隔符來決議 CSV:
import pandas as pd
datas_path = 'target/youtube_videos.csv'
df = pd.read_csv(datas_path, sep=';')
print(df)
這正確決議了被 Numpy 錯誤決議的文本行。不幸的是,Numpy 在決議文本時有點脆弱。但是,由于Pandas 在幕后使用 Numpy,因此可以輕松地調整任何 Pandas 物件(即 Series 和 DataFrames)以與 Numpy 相關例程一起使用。
參考:
- 原評論。
- 大熊貓檔案上
read_csv。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/349960.html
下一篇:矢量化嵌套vmap
