我正在嘗試撰寫一個不必每年都輸入的代碼,代碼只是為我做。我嘗試使用 for 回圈,for year in range(1995,2021):但代碼給了我以下錯誤
File "C:\Users\chadd\OneDrive\Desktop\Wind Spacecraft\Codes\get_files_wind_test.py", line 38, in <module>
sub_dir = year '/'
# sub directory because there are several year in the parent directory
TypeError: unsupported operand type(s) for : 'int' and 'str'
下面是我沒有 for 回圈的代碼。在這里我手動輸入年份,它會從網上下載檔案并保存在特定的年份。我正在考慮用year=input("Enter the year:")for 回圈替換。
###############
## Define sc and date range
###############
year = input("Enter the year: ") # takes the input
###############
## Define Paths
###############
external_url_base = 'https://cdaweb.gsfc.nasa.gov/pub/data/wind/waves/dust_impact_l3/' # url from where we need to scarp our data
sub_dir = year '/' # sub directory because there are several year in the parent directory
url = external_url_base sub_dir
local_dir_base = r'C:\Users\chadd\OneDrive\Desktop\Wind Spacecraft\Data' # this is my directory where files will be saved
sub_dir = '/' year '/' # since there are years ranging from 1995 to 2020,as the input change different year files get stored in different year folder.
local_dir = local_dir_base sub_dir # this line compiles local base and sub as one and this is the path that python uses to save files.
#########################
# Identify remote files #
#########################
## Read web page
resp = requests.get(url)
# create beautiful-soup object (all links on web page)
soup = BeautifulSoup(resp.content, 'html5lib')
## Error handle
if resp.status_code != 200:
print('**ERROR: No data available from then**')
resp.raise_for_status()
# create beautiful-soup object (all links on web page)
soup = BeautifulSoup(resp.content, 'html5lib')
# find all links on web-page
links = soup.findAll('a')
# filter the link sending with .cdf
cdf_files = []
for l in links:
if l['href'].endswith('cdf'):
#print(l['href'])
cdf_files.append(url l['href'])
print(cdf_files)
###############
## Go get remote files, download locally
###############
# Iterate through list of files
for link in cdf_files:
#print(link)
# get the file name seperately
fn = link.split('/')[-1]
r = requests.get(link)
#print(r)
# Sub directory based on type of data being downloaded, to be saved in the Data directory. ex: TDS_files, QF_files, etc.
data_file = local_dir fn
# Check if the file is already in that directory to avoice duplicates and uneccisary processing.
if path.exists(data_file):
print('Already have ',data_file, '.\nMoving on...')
continue
else:
print('Downloading ',data_file, '...')
with open(data_file, 'wb') as f:
f.write(r.content)
任何幫助表示贊賞!
uj5u.com熱心網友回復:
這里 year 是一個int(數字),您需要使用sub_dir = str(year) '/'.將其轉換(轉換)為字串(文本)。
這應該可以解決您在 for 回圈中的問題。
uj5u.com熱心網友回復:
錯誤
是一個“操作員”。也就是說,它對它兩邊的東西進行操作。我們稱這些東西為“運算元”。因此錯誤的措辭。
左邊有一個int(整數),右邊有一個str(一些文本)。雖然我們人類并沒有真正將數字和符號視為完全不同的東西,但它們對于計算機來說卻是完全不同的東西。文字符號“1”與實際的數字1完全不同。所以,對于計算機來說,說出來words plus 123是沒有意義的。
因此,您需要告訴 Python 將您的數字(年份)轉換為文本。最快的方法是將數字包裝在str().
修復它
tl; dr:將第 38 行更改為:
sub_dir = str(year) '/'
您還必須將第 42 行編輯為 sub_dir = '/' str(year) '/'
有時,Python 會很聰明,會自動將事物轉換為正確的型別(可能是因為另一個程式員已經完成了上述操作并添加了這種轉換),但在這種情況下卻不是。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/404989.html
標籤:
