[英]How do I read the first 100 lines of a json metadata file and write them to a smaller json file? [Python]
[英]How to read/print the header (first 100 lines) of a netCDF file in Python?
我一直在尝试阅读 Python 中 netCDF 文件的 header(前 100 行),但遇到了一些问题。 I am familiar with the read_nc
function available in the synoptReg package for R and with the ncread
function that comes with MATLAB, as well as the read_csv
function available in the pandas library. 然而,据我所知,netCDF (.nc) 文件没有任何类似之处。
注意到这一点,并使用这个问题的答案,我尝试了以下方法(没有成功):
with open(filepath,'r') as f:
for i in range(100):
line = next(f).strip()
print(line)
但是,我收到此错误,即使我已确保制表符未与空格混合并且for
语句位于with
块内( 如该问题的最佳答案所给出的解释):
'utf-8' codec can't decode byte 0xbb in position 411: invalid start byte
我还尝试了以下方法:
with open(filepath,'r') as f:
for i in range(100):
line = [next(f) for i in range(100)]
print(line)
和
from itertools import islice
with open('/Users/toshiro/Desktop/Projects/CCAR/Data/EDGAR/v6.0_CO2_excl_short-cycle_org_C_2010_TOTALS.0.1x0.1.nc','r') as f:
for i in range(100):
line = list(islice(f, 100))
print(line)
但收到与上述相同的错误。 有什么解决方法吗?
你不能。 netCDF 是二进制文件,不能解释为文本。
如果文件是netCDF3
编码的,您可以使用scipy.io.netcdf_file
读取它们。 但它们更有可能是netCDF4
,在这种情况下,您将需要netCDF4
package。
除此之外,我强烈推荐使用xarray
package 来读取和处理 netCDF 数据。 它支持带标签的 N 维数组接口——想想 pandas 数组的每个维度上的 numpy 索引。
无论您是使用 netCDF 还是 xarray 的 go,netCDF 都是自描述的并支持任意读取,因此您无需加载整个文件即可查看元数据。 类似于查看文本文件的头部,您可以简单地执行以下操作:
import xarray as xr
ds = xr.open_dataset("path/to/myfile.nc")
print(ds) # this will give you a preview of your data
此外,xarray 确实有一个xr.Dataset.head
function ,它将沿每个维度显示前 5 个(如果您提供 int,则为 N 个)元素:
ds.head() # display a 5x5x...x5 preview of your data
有关详细信息,请参阅阅读和编写 netCDF 文件的入门指南和用户指南部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.