【发布时间】:2018-07-02 10:11:47
【问题描述】:
我对使用 python 很陌生,我试图将 CSV 文件的负载(100 秒)读入一个数据帧。但是,csv 文件非常混乱,使用多个分隔符等。我试着搜索这个网站,但我发现的所有东西都不起作用。我已经尝试了许多选项的 readlines 和 pd.read,但我得到的只是错误或空数据帧。当我在 excel 中打开 CSV 时,它看起来很好,当我将它保存为 UTF-8 csv 时,一切正常。但是,即使使用宏,对每个 excel 文件执行此操作也是如此。有没有办法使用python代码复制这个过程,例如in2csv?下面我提供了我需要使用的 csv 文件的一部分,以及来自 excel 的部分 csv(有效)。对我来说,主要区别似乎是空格和逗号分隔符,但在 pd.read 中更改它并没有帮助。非常感谢!
凌乱的 csv:
"Device name:UU-WGB-JV_1 Device type:SUN2000 Device address:IP Address=62.72.193.88 Device No.=2 Date:2018-01-23 08:51:23 "
"Generated On" "Device Status" "Energy Yield of Current Day (kWh)" "Inv. efficiency"(%) "Total Energy Yield (kWh)" "Input Power (kW)" "Active Power (kW)" "Reactive Power (kVar)" "Power Factor" "Grid Frequency (Hz)" "Grid A Current (A)" "Grid B Current (A)" "Grid C Current (A)" "Grid A Phase Voltage (V)" "Grid B Phase Voltage (V)" "Grid C Phase Voltage (V)" "PV1 Input Current (A)" "PV2 Input Current (A)" "PV3 Input Current (A)" "PV4 Input Current (A)" "PV5 Input Current (A)" "PV6 Input Current (A)" "PV1 Input Voltage (V)" "PV2 Input Voltage (V)" "PV3 Input Voltage (V)" "PV4 Input Voltage (V)" "PV5 Input Voltage (V)" "PV6 Input Voltage (V)" "Cabinet Temperature (℃)"
"2017-12-22 00:00:00 " "Idle: No irradiation" "0.00" "0.00" "45803.34" "0.000" "0.000" "0.000" "0.000" "0.00" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0"
"2017-12-22 00:15:00 " "Idle: No irradiation" "0.00" "0.00" "45803.34" "0.000" "0.000" "0.000" "0.000" "0.00" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0"
好的 CSV:
Device name:UU-CB_1 Device type:SUN2000 Device address:IP Address=62.140.137.136 Device No.=1 Date:2018-01-22 13:31:51 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Generated On,Device Status,Energy Yield of Current Day (kWh),Inv. efficiency(%),Total Energy Yield (kWh),Input Power (kW),Active Power (kW),Reactive Power (kVar),Power Factor,Grid Frequency (Hz),Grid A Current (A),Grid B Current (A),Grid C Current (A),Grid A Phase Voltage (V),Grid B Phase Voltage (V),Grid C Phase Voltage (V),PV1 Input Current (A),PV2 Input Current (A),PV3 Input Current (A),PV4 Input Current (A),PV5 Input Current (A),PV6 Input Current (A),PV1 Input Voltage (V),PV2 Input Voltage (V),PV3 Input Voltage (V),PV4 Input Voltage (V),PV5 Input Voltage (V),PV6 Input Voltage (V),Cabinet Temperature (℃)
"2017-11-01 00:00:00 ",Idle: No irradiation,0,-,36670.07,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
【问题讨论】:
-
第一个是定长文件,不是CSV。 Excel 可以很好地猜测格式,但会失败,例如,如果您在使用
,作为小数点分隔符和;作为列表分隔符的欧洲国家工作。 -
对于混乱的 CSV 和多个分隔符,是的。这正是数据工程和数据科学的意义所在——80% 正在清理数据。例如,即使是“好”的 CSV 也有混乱的日期。
Inv. efficiency(%)字段中的-是什么?还是 header 中的许多,,,条目?看起来有人试图将标题视为列行。您必须为每个文件指定适当的格式(平面、分隔等),并在适当的地方跳过标题。 -
为避免指定多种格式,一个技巧是将尽可能多的文件转换为通用格式。例如,在定长文件中,您可以将所有
" "实例转换为","或很少使用的字符,如 ¤。不过,您必须对此进行广泛测试并保留备份副本以防出错。例如,平面文件头似乎有 不同的个空格。 -
- 表示没有价值。我主要对当天的能源产量感兴趣,所以这没问题。当我在这个文件上使用 pd.read_csv(file, header = 1, index_col = 'Generated On',parse_dates = True) 时,它对我来说非常可行。所以我必须将固定长度(或宽度)转换为 csv 文件?它会自动保存为 .csv。
标签: python pandas csv encoding utf-8