gpt4 book ai didi

python - 获取在 ubuntu 上运行 python 的无效语法

转载 作者:行者123 更新时间:2023-12-04 18:59:33 24 4
gpt4 key购买 nike

我已经在 Windows 10 上使用 spyder 测试了该脚本,它运行良好。
现在我试图在 ubuntu 虚拟主机上运行它,它给了我一个无效的语法错误。
python版本为Python 3.5.2 (我更新后得到的最新版本)。 Ubuntu版本是

Description:    Ubuntu 16.04.7 LTS
Release: 16.04
Codename: xenial
我知道它已经过时了,但我不能自己更新它......
一开始就出现问题的代码很简单:
# Begin of time period. Format: YYYY-mm-dd
date_from = f'{year}-01-01'
我正在使用“python my_file.py”对其进行测试

最佳答案

F 字符串只能使用 Python 3.6及以上如 new features of Python 3.6: 中所述

New FeaturesPEP 498 introduces a new kind of string literals: f-strings, or formatted string literals.Formatted string literals are prefixed with 'f' and are similar to the format strings accepted by str.format(). They contain replacement fields surrounded by curly braces. The replacement fields are expressions, which are evaluated at run time, and then formatted using the format() protocol:


要解决此问题,您可以使用 str.format() 改为: date_from = '{}-01-01'.format(year)或者你可以升级你的 Python: sudo apt install python3.7

关于python - 获取在 ubuntu 上运行 python 的无效语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65231411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com