简体   繁体   English

SyntaxError:python 中的语法无效

[英]SyntaxError: invalid syntax in python

I am trying to include the year to url .我正在尝试将年份包含在url I get syntax is invalid.我得到语法无效。 How can I include the year in my URL.如何在我的 URL 中包含年份。

year, month, day, hour = time_point.asStrTuple()
if analysis:
    url = fileinfo['url_anl']
else:
    url = fileinfo['url_fct']

Year = year
year_month = year + month
year_month_day = year_month + day
year_month_day_hour = year_month + day + hour
url += Year + '/' year_month + '/' + year_month_day + '/' + year_month_day_hour + '/'

if analysis:
    url += fileinfo['filecode_anl']
else:
    url += fileinfo['filecode_fct']
url += year_month_day + '_'

It should be它应该是

url += Year + '/' + year_month + '/' + year_month_day + '/' + year_month_day_hour + '/'

You forgot + two times:你忘记了+两次:

'/' year_month 
'/'year_month_day_hour 

But if you are doing string manipulation it is better to use format https://docs.python.org/3.6/library/stdtypes.html#str.format但是,如果您正在进行字符串操作,最好使用format https://docs.python.org/3.6/library/stdtypes.html#str.format

url += '{}/{}/{}/{}/'.format(Year, year_month, year_month_day, year_month_day_hour)

Or in your case you can do this with join https://docs.python.org/3.6/library/stdtypes.html#str.join或者在您的情况下,您可以通过join https://docs.python.org/3.6/library/stdtypes.html#str.join来执行此操作

url += '/'.join([Year, year_month, year_month_day, year_month_day_hour, ''])

Note that for join I added '' empty string to list, so that there will be ending /请注意,对于 join 我添加了''空字符串到列表中,这样就会有结尾/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM