PythonL: invalid syntax file “<fstring>”, line 1

若如初见. 提交于 2019-12-22 04:31:15

问题


When doing python3 Webhook.py (this is the file), it gives me the error: File "<fstring>", line 1 (%X - %x) ^ SyntaxError: invalid syntax

I've tried to print out the raw contents of the file and I also used a hex editor, there is nothing on line 1 that should be causing erorrs. I also did: import time, os, aiohttp, plistlib, discord, asyncio, json, subprocess In the Terminal.app version of Python3 and I had no errors, my version was 3.6.3 but updated to 3.6.5 to check if the issue would go away, which didn't. Can anyone help?


回答1:


I just spent 1 hour reviewing my code with the same issue. In my case I started removing parts of the code to narrow down the problem.

Finally I to the root cause of the problem.

in my case I was printing an f"string" and inside the f string I had a space in the name of the variable I was calling example print(f"This is a statement {Var 23} "

That space, generated my problem.

I hope this helps :)




回答2:


This happens when anything inside {} is not valid, in a string formatted using the f string formatting prefix. Python 3.7 in my case. The upside is you get the string causing the problem on the error message. You don't get the line number, but it's still easy to figure out once you acknowledge that line number 1 is not the correct line number of the error.



来源:https://stackoverflow.com/questions/49582183/pythonl-invalid-syntax-file-fstring-line-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!