x = " \{ Hello \} {0} "
print x.format(42)
Run Code Online (Sandbox Code Playgroud)
给我 : Key Error: Hello\\
我想打印输出: {Hello} 42
我想int加入一个string.这就是我现在正在做的事情:
num = 40
plot.savefig('hanning40.pdf') #problem line
Run Code Online (Sandbox Code Playgroud)
我必须运行几个不同数字的程序,而不是两个40.所以我想做一个循环但插入这样的变量不起作用:
plot.savefig('hanning', num, '.pdf')
Run Code Online (Sandbox Code Playgroud)
如何将变量插入Python字符串?
在使用 box bento/centos-7 的新 Vagrant VM 上,以下命令破坏了我的 pip 安装:
yum update
yum install epel-release -y
yum install python-pip -y
/usr/bin/pip2 install --upgrade pip setuptools pyOpenSSL psycopg2-binary lxml
Run Code Online (Sandbox Code Playgroud)
这最终失败了
Downloading https://files.pythonhosted.org/packages/84/48/5c99d8770fd0a9eb0f82654c3294aad6d0ba9f8600638c2e2ad74f2c5d52/setuptools-52.0.0.tar.gz (2.1MB)
100% |????????????????????????????????| 2.1MB 821kB/s
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "setuptools/__init__.py", line 16, in <module>
import setuptools.version
File "setuptools/version.py", line 1, in <module>
import pkg_resources
File "pkg_resources/__init__.py", line 1367
raise SyntaxError(e) from e
^
SyntaxError: invalid …Run Code Online (Sandbox Code Playgroud) 我想知道如何使用f-strings以Pythonic方式格式化这种情况:
names = ['Adam', 'Bob', 'Cyril']
text = f"Winners are:\n{'\n'.join(names)}"
print(text)
Run Code Online (Sandbox Code Playgroud)
问题是'\'不能在f-string中使用.预期产量:
Winners are:
Adam
Bob
Cyril
Run Code Online (Sandbox Code Playgroud) 我开始使用 bitbucket CI 制作应用程序,我正在使用以下步骤来部署应用程序,但安装 pip 的步骤失败了。
script:
- apt-get update
- apt-get install -y python-dev
- curl -O https://bootstrap.pypa.io/get-pip.py
- python get-pip.py
... and a few more steps
Run Code Online (Sandbox Code Playgroud)
不知道为什么,但python get-pip.py步骤失败并出现以下错误。
Traceback (most recent call last):
File "get-pip.py", line 24226, in <module>
main()
File "get-pip.py", line 199, in main
bootstrap(tmpdir=tmpdir)
File "get-pip.py", line 82, in bootstrap
from pip._internal.cli.main import main as pip_entry_point
File "/tmp/tmpUgc5ng/pip.zip/pip/_internal/cli/main.py", line 60
sys.stderr.write(f"ERROR: {exc}")
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
直到昨天,这一切正常。不知道为什么这现在不起作用。
我认为这可能是因为 Windows,但我检查了运行 linux 的本地机器,但这些步骤但它们工作正常。
我最近一直在学习python 3,我无法得到任何涉及字符串插值(格式化)的例子.
In [1]: state = "Washington"
In [2]: state
Out[2]: 'Washington'
In [3]: my_message = f"I live in {state}"
File "<ipython-input-3-d004dd9e0255>", line 1
my_message = f"I live in {state}"
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我认为我的机器默认为python 2,但快速检查显示:
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
Type "copyright", "credits" or "license" for more information.
IPython 5.2.2 -- An enhanced Interactive Python.
Run Code Online (Sandbox Code Playgroud)
我在Ubuntu 16.04:
python3 --version
Python 3.5.2
Run Code Online (Sandbox Code Playgroud)
我只是忽略了基本语法?我在同学的几台计算机上运行相同的命令,似乎执行得很好.
我知道那F Strings是在Python 3.6. 为此,我收到了错误 -Invalid Syntax
DATA_FILENAME = 'data.json'
def load_data(apps, schema_editor):
Shop = apps.get_model('shops', 'Shop')
jsonfile = Path(__file__).parents[2] / DATA_FILENAME
with open(str(jsonfile)) as datafile:
objects = json.load(datafile)
for obj in objects['elements']:
try:
objType = obj['type']
if objType == 'node':
tags = obj['tags']
name = tags.get('name','no-name')
longitude = obj.get('lon', 0)
latitude = obj.get('lat', 0)
location = fromstr(F'POINT({longitude} {latitude})', srid=4326)
Shop(name=name, location = location).save()
except KeyError:
pass
Run Code Online (Sandbox Code Playgroud)
错误 -
location = (F'POINT({longitude} {latitude})', srid=4326)
^
SyntaxError: …Run Code Online (Sandbox Code Playgroud) 我收到 SyntaxError:f-string: empty expression not allowed我不确定(完全)这意味着什么
我试过在代码周围移动并在网上查看但我没有得到不同的结果
代码是:
@client.command()
async def load(ctx, extension):
client.load_extension(f'cogs.{extension}')
await ctx.send(f"Loaded the {} module!".format(extension))
Run Code Online (Sandbox Code Playgroud)
它是用于齿轮的,我确定我已经把其他一切都做对了,但我不确定
如果有人知道该怎么做,请告诉我,谢谢
我有一个名为method的变量,它的值是POST,但是当我尝试运行时,print(f"{method} method is used.")它会在最后一个双引号处给出语法错误,但我找不到它为什么会这样做的原因.我使用的是python 3.5.2.
我在使用Python 3.7.
我在第3行的代码工作正常,但是当我将基础公式插入第4行时,我的代码返回错误:
SyntaxError:f-string:mismatched'(','{'或'[' (错误指向第一个'('第4行'.
我的代码是:
cheapest_plan_point = 3122.54
phrase = format(round(cheapest_plan_point), ",")
print(f"1: {phrase}")
print(f"2: {format(round(cheapest_plan_point), ",")}")
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚第4行有什么问题.
python ×11
python-3.x ×5
string ×3
f-string ×2
pip ×2
curly-braces ×1
format ×1
newline ×1
python-3.6 ×1
ubuntu-16.04 ×1
variables ×1