【发布时间】:2022-01-03 21:41:04
【问题描述】:
我正在尝试打开一个 .gz 文件(它只是 .gz,而不是 .log.gz 或其他任何文件),但逐行读取文件只会显示乱码。它应该有传感器数据。
这是我的代码:
import gzip
with gzip.open('data.gz','r') as fin:
for line in fin:
print('got line', line)
这是一个输出示例:
b'\x80\x04\x95\x90\x01\x00\x00\x00\x00\x00\x00\x8c\x1bsklearn.preprocessing._data\x94\x8c\x0eStandardScaler\x94\x93\x94)\x81\x94}\x94(\x8c\twith_mean\x94\x88\x8c\x08with_std\x94\x88\x8c\x04copy\x94\x88\x8c\x0en_features_in_\x94K\x08\x8c\x0fn_samples_seen_\x94\x8c\x15numpy.core.multiarray\x94\x8c\x06scalar\x94\x93\x94\x8c\x05numpy\x94\x8c\x05dtype\x94\x93\x94\x8c\x02i8\x94K\x00K\x01\x87\x94R\x94(K\x03\x8c\x01<\x94NNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00t\x94bC\x08\xae\x0b\x00\x00\x00\x00\x00\x00\x94\x86\x94R\x94\x8c\x05mean_\x94\x8c\x13joblib.numpy_pickle\x94\x8c\x11NumpyArrayWrapper\x94\x93\x94)\x81\x94}\x94(\x8c\x08subclass\x94h\r\x8c\x07ndarray\x94\x93\x94\x8c\x05shape\x94K\x08\x85\x94\x8c\x05order\x94\x8c\x01C\x94\x8c\x05dtype\x94h\x0f\x8c\x02f8\x94K\x00K\x01\x87\x94R\x94(K\x03h\x13NNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00t\x94b\x8c\n'
b'allow_mmap\x94\x88ub*\xd4\xff\xe6\xbd\rE@\x15\xd1=A5\xdfg@\x0f\x7fv\x94\xabjg@\x04?\xf0\x03?0^@v\x94\xab\xda\xb5\xb6E@\x9bz\xec\xdd\x90\x9e?@\x13\xd7\xa8J\x94\x92;@*\x1f\xc6\xfd\xba$7@\x95&\x00\x00\x00\x00\x00\x00\x00\x8c\x04var_\x94h\x1b)\x81\x94}\x94(h\x1eh h!K\x08\x85\x94h#h$h%h(h*\x88ub\x8f\x16F\x8b\xed\n'
b'T@\x01^\xd6u\xfd\x94\xa9@Z\xae\xc2d\xb0\x05\x9a@\xe7\xbb\xeeu.$\x85@~\xd7\xe2\xe7_@F@\xb0$HdS\x17F@*\xa8\x07o\x16]}@\x1e\xba\xc0\xc8r]<@\x95(\x00\x00\x00\x00\x00\x00\x00\x8c\x06scale_\x94h\x1b)\x81\x94}\x94(h\x1eh h!K\x08\x85\x94h#h$h%h(h*\x88ub\x8fDh\n'
b'Z\xe8!@E\xaa1\xf0\x91\x9cL@Q\xecq~\xa0gD@\x13m$\x9e\x92\x02:@\xe9>L\x19(\xaf\x1a@DZ\xf9\xbd\x7f\x96\x1a@~\x014y\xdf\xac5@y\xa3\xc0X\xb4M\x15@\x95\x1f\x00\x00\x00\x00\x00\x00\x00\x8c\x10_sklearn_version\x94\x8c\x060.24.1\x94ub.'
【问题讨论】:
-
gz是一种压缩文件 (gzip)。你必须gunzip it to read it。 -
我们需要检查数据来重现这一点。表面上最明显的解释是数据文件并不真正包含 gzip 压缩的文本。您能否构建一个适用于已知良好数据的minimal reproducible example(例如,其中包括有关如何使用
printf '%s\n' "first line" "second line" | gzip >data.gz等明显正确的过程创建数据文件的说明)? -
见replit.com/@CharlesDuffy2/OblongScaredClient#main.sh——当我们创建一个已知良好的输入文件时,代码可以正常工作