使用Python,文件 [英] Working with Python, files
问题描述
我有一些需要读取的数据文件.我知道我应该使用数据集,但是有一种方法可以下载这些文件,而无需手动下载它们,而是通过其URL?就我而言情况如何.我正在使用conda-python和netCDF4.无论如何,我都无法读取这些文件.对不起我的英语不好.来源是 http://meop40. troja.mff.cuni.cz:11180/gw.projekt/data.stratopauza/netcdf.profily/
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/
我的第一次尝试:
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')
另一种尝试:
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**
推荐答案
也许将内容保存到临时文件中?
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())
现在文件./tempfile
可以正常使用
这篇关于使用Python,文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!