【发布时间】:2021-12-27 01:47:15
【问题描述】:
我正在尝试使用 genfromtxt() 将文本文件中的数据导入 python。 我目前拥有的代码是
lowResOmni = np.genfromtxt('omni low res 7-14 to 7-18.txt', dtype=[('year', int), ('SOY', float)
, ('B', float), ('Bx', float), ('By', float), ('Bz', float)
, ('plasmaTemp', float), ('ionDensity', float), ('plasmaSpeed', float), ('plasmaPressure', float)
, ('pFlux1', float), ('pFlux2', float), ('pFlux4', float), ('pFlux10', float)
, ('DST', int), ('AL', int), ('AU', int)]
, comments="#", skip_header=2, usemask=True
, missing_values={0:'', 1:''
, 2:999.9, 3:999.9, 4:999.9, 5:999.9
, 6:9999999., 7:999.9, 8:9999., 9:99.99
, 10:999999.99, 11:99999.99, 12:99999.99, 13:99999.99
, 14:99999, 15:99999, 16:99999})
每当 .txt 文件的行以 # 开头时,它都会给我一个IndexError: list index out of range。我已经在删除注释行的 .txt 文件上尝试了这个确切的代码,它工作正常,但我宁愿没有两个单独的文件用于数据和 cmets。
例如
TIME_AT_CENTER_OF_HOUR 1AU_IP_MAG_AVG_B 1AU_IP_BX,_GSE 1AU_IP_BY,_GSM 1AU_IP_BZ,_GSM 1AU_IP_PLASMA_TEMP 1AU_IP_N_(ION) 1AU_IP_PLASMA_SPEED 1AU_IP_FLOW_PRESSURE 1AU_PROTONS>1_MEV 1AU_PROTONS>2_MEV 1AU_PROTONS>4_MEV 1AU_PROTONS>10_MEV 1-H_DST 1-H_AL-INDEX AU-INDEX
Year____Secs-of-year nT nT nT nT Deg_K Per_cc Km/s nPa 1/(SQcm-ster-s) 1/(SQcm-ster-s) 1/(SQcm-ster-s) 1/(SQcm-ster-s) nT nT nT
2000 16849800.000000 5.50000 -4.90000 -0.800000 -1.20000 321609. 2.80000 606.000 1.92000 614.000 156.000 25.5000 1.87000 -29 -279 234
2000 16853400.000000 4.30000 -2.90000 -2.90000 0.400000 200127. 3.40000 611.000 2.42000 625.000 159.000 26.2000 1.91000 -20 -245 164
2000 16857000.000000 3.90000 -2.10000 -2.50000 1.40000 174932. 3.70000 615.000 2.70000 549.000 142.000 23.2000 1.79000 -12 -264 113
2000 16860600.000000 3.60000 -1.30000 -2.40000 0.600000 148701. 3.40000 616.000 2.61000 492.000 125.000 20.5000 1.62000 -14 -155 109
2000 16864200.000000 4.10000 -1.00000 -2.20000 0.500000 116372. 2.70000 614.000 2.20000 485.000 124.000 20.5000 1.73000 -20 -140 89
2000 16867800.000000 4.30000 -1.40000 -1.00000 -3.60000 96452.0 2.50000 607.000 1.91000 465.000 119.000 19.5000 1.63000 -19 -275 240
#
# Key Parameter and Survey data (labels K0,K1,K2) are preliminary browse data.
# Generated by CDAWeb on: Mon Nov 15 15:35:02 2021
工作正常,但是
# now we have an error for some reason
TIME_AT_CENTER_OF_HOUR 1AU_IP_MAG_AVG_B 1AU_IP_BX,_GSE 1AU_IP_BY,_GSM 1AU_IP_BZ,_GSM 1AU_IP_PLASMA_TEMP 1AU_IP_N_(ION) 1AU_IP_PLASMA_SPEED 1AU_IP_FLOW_PRESSURE 1AU_PROTONS>1_MEV 1AU_PROTONS>2_MEV 1AU_PROTONS>4_MEV 1AU_PROTONS>10_MEV 1-H_DST 1-H_AL-INDEX AU-INDEX
Year____Secs-of-year nT nT nT nT Deg_K Per_cc Km/s nPa 1/(SQcm-ster-s) 1/(SQcm-ster-s) 1/(SQcm-ster-s) 1/(SQcm-ster-s) nT nT nT
2000 16849800.000000 5.50000 -4.90000 -0.800000 -1.20000 321609. 2.80000 606.000 1.92000 614.000 156.000 25.5000 1.87000 -29 -279 234
2000 16853400.000000 4.30000 -2.90000 -2.90000 0.400000 200127. 3.40000 611.000 2.42000 625.000 159.000 26.2000 1.91000 -20 -245 164
2000 16857000.000000 3.90000 -2.10000 -2.50000 1.40000 174932. 3.70000 615.000 2.70000 549.000 142.000 23.2000 1.79000 -12 -264 113
2000 16860600.000000 3.60000 -1.30000 -2.40000 0.600000 148701. 3.40000 616.000 2.61000 492.000 125.000 20.5000 1.62000 -14 -155 109
2000 16864200.000000 4.10000 -1.00000 -2.20000 0.500000 116372. 2.70000 614.000 2.20000 485.000 124.000 20.5000 1.73000 -20 -140 89
2000 16867800.000000 4.30000 -1.40000 -1.00000 -3.60000 96452.0 2.50000 607.000 1.91000 465.000 119.000 19.5000 1.63000 -19 -275 240
#
# Key Parameter and Survey data (labels K0,K1,K2) are preliminary browse data.
# Generated by CDAWeb on: Mon Nov 15 15:35:02 2021
突然发作。
有趣的是,文件底部的 cmets 不会引起问题。
感谢任何输入!
【问题讨论】:
-
只是为了确定,您使用的是哪个操作系统以及您的文件有什么样的行尾? LF 还是 CRLF?
-
@Nullman python 3.7.5 on mac 11.2,.txt文件有LF行结尾,是UTF-8编码
-
你试过
skip_header=3吗?我不确定哪个先发生,跳过标题或跳过注释行。您可能需要显示完整的回溯,以便我们(和您)可以看到错误发生的位置。 -
@hpaulj 好的,所以
skip_header=3起作用了,我想这只是没有考虑注释中以 # 开头的行。但后来我很困惑为什么comments='#'参数不起作用。 -
它必须是先跳过标题行,然后再检查cmets。
标签: python numpy comments genfromtxt