2014-05-11 17 views
0

我對Python和編程一般都很陌生,真的很感謝關於這個可能非常簡單的問題的指導。如何在Python中保存包含用戶輸入年份的文件

我使用的代碼:

year = input (Choose a year between 2001 and 2010) 

然後我想下名稱,其中包括當年追加這些空列表(如2003_woodland.shp下這些名字對於2001年的文件,至2010年已經存在於我工作空間)

File_list = [ ] 

File_list.append(path+ "{year}_woodland.shp") 

File_list.append(path + "{year}perimeters.shp") 

如何格式化它以獲取用戶輸入的年份?我需要製作一個循環嗎?

回答

0

插入一個字符串到另一個字符串中使用了此語法:需要你的代碼進行

string2 = 'hello world' 
print('contents of string2: %s' % string2) 

只有輕微的變化:

year = input('Choose a year between 2001 and 2010: ') 

File_list = [ ] 

File_list.append(path + ("%s_woodland.shp" % year)) 

File_list.append(path + ("%sperimeters.shp" % year)) 
+0

這也適用於其他的文字,例如: ''五:%i'%5'。 請注意,%符號後面的字母是文字類型的第一個字母:%s代表字符串,%i代表整數,%f代表float。 –

相關問題