Working with Python, files

允我心安 提交于 2019-12-10 19:27:00

问题


I have some data files which I need to read. I know I should use Dataset, but is there a way how to download these files without downloading them manually but by its URL? How would it look like in my case. I am working with conda-python and netCDF4. Whatever I do I cannot read these files. Sorry for my English. The source is http://meop40.troja.mff.cuni.cz:11180/gw.projekt/data.stratopauza/netcdf.profily/

My first try:

from netCDF4 import Dataset
import numpy as np

my_example_nc_file = '/Users/Leif/Downloads/my_example_nc_data.nc'
fh = Dataset(my_example_nc_file, mode='r') 

Another Try:

from mpl_toolkits.basemap import Basemap, shiftgrid, cm
import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset

url = 'http://meop40.troja.mff.cuni.cz:11180/gw.projekt/data.stratopauza/netcdf.profily/atmPrf_C001.2010.227.00.03.G04_2013.3520_nc '
etopodata = Dataset(url) **Error**

回答1:


Maybe save the contents to a temporary file?

import urllib.request

response = urllib.request.urlopen(url)

with open("./tempfile", "w") as f:
    f.write(response.read())

Now the file ./tempfile can be used normally



来源:https://stackoverflow.com/questions/44622315/working-with-python-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!