1 回答
TA貢獻1825條經驗 獲得超4個贊
解決了
我查看了 xarray readthedocs 頁面,在 open_mfdataset 頁面中看到了這個簡介。
路徑(str 或 sequence)——“path/to/my/files/*.nc”形式的字符串 glob 或要打開的文件的顯式列表。路徑可以作為字符串或 pathlib 路徑給出。如果需要沿多個維度進行連接,則路徑必須是嵌套的列表列表(有關詳細信息,請參閱 manual_combine)。(字符串 glob 將擴展為一維列表。)
因此,我通過了一個列表
更新和工作代碼
import xarray as xr
from datetime import timedelta, date, datetime
import pandas as pd
import numpy as np
# **************
# Date Ranges
# **************
def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield start_date + timedelta(n)
# Start & End Date
start_date = date(2019, 1, 1)
end_date = date(2019, 1, 31)
# Empty List
filepath = 'C:\\Users\\USER\\FILES\\'
filelist = []
# Loop through all MERRA2 files and add the ones we need to the list
for single_date in daterange(start_date, end_date):
YYYY = single_date.strftime("%Y")
MM = single_date.strftime("%m")
DD = single_date.strftime("%d")
filename = filepath + YYYY + MM + DD + '_data_Nx.nc'
filelist.append(filename)
# Merge via X-Array and export to csv
ds = xr.open_mfdataset(filelist, combine='by_coords')
df = ds.to_dataframe()
df.to_csv('export.csv', index=True)
添加回答
舉報
