Open
Description
Is your feature request related to a problem?
When I use netCDF4.Dataset
to open a file whose path contains Chinese characters, I get:
import netCDF4
file = "文件.nc"
ds = netCDF4.Dataset(file)
# FileNotFoundError: [Errno 2] No such file or directory: '文件.nc'
If I specify encoding='gbk'
, it opens correctly:
ds = netCDF4.Dataset(file, encoding='gbk')
However, xarray’s open_dataset
does not support an encoding
argument:
import xarray as xr
file = "文件.nc"
ds = xr.open_dataset(file)
# FileNotFoundError: [Errno 2] No such file or directory: 'E:\\netcdf\\文件.nc'
ds = xr.open_dataset(file, engine='netcdf4', encoding='gbk')
# TypeError: NetCDF4BackendEntrypoint.open_dataset() got an unexpected keyword argument 'encoding'
Related netCDF4 issue comment
(from netCDF4-python#997)
Describe the solution you'd like
I would like xarray to accept an encoding
parameter so that files with Chinese paths open successfully.
Describe alternatives you've considered
No response
Additional context
No response
Activity
welcome commentedon May 28, 2025
Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!
wqshen commentedon May 28, 2025
You can change following lines in xarray source code
https://github.com/pydata/xarray/tree/main/xarray/backends/netCDF4_.py
and
pay attention to all occurrence of
encoding
.dcherian commentedon May 28, 2025
@wqshen We would happily take a PR to enable this. Are you able to contribute?