macos 调用 URL 时 Python 中的非 ASCII 字符语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2591580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Non-ASCII character Syntax Error in Python while calling URL
提问by Apache
How can data can be sent to the server?
如何将数据发送到服务器?
For example, I have retrieve MAC address, so I want send to the server (e.g. http://211.21.24.43:8080/data?mac=00-0C-F1-56-98-AD)
例如,我已经检索到 MAC 地址,所以我想发送到服务器(例如http://211.21.24.43:8080/data?mac=00-0C-F1-56-98-AD)
I found this snippet on the Internet:
我在互联网上找到了这个片段:
from urllib2 import Request, urlopen
from binascii import b2a_base64
def b64open(url, postdata):
req = Request(url, b2a_base64(postdata), headers={'Content-Transfer-Encoding': 'base64'})
return urlopen(req)
conn = b64open("http://211.21.24.43:8080/data","mac=00-0C-F1-56-98-AD")
but when I run it, I get:
但是当我运行它时,我得到:
File "send2.py", line 8
SyntaxError: Non-ASCII character '\xc3' in file send2.py on line 8, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Can anyone help me send data to the server?
谁能帮我将数据发送到服务器?
Thanks in advance
提前致谢
采纳答案by S.Lott
You have copied and pasted code with non-ASCII characters. You have an \xc3
character on line 8.
您已使用非 ASCII 字符复制和粘贴代码。您\xc3
在第 8 行有一个字符。
回答by Rodrigo Pinho Pereira de Souza
put the encoding at the top of .py file
将编码放在 .py 文件的顶部
example:
例子:
#!/usr/bin/env python
#coding: utf8
import os
...
This probably happened because you copied/pasted some unicode character that is not visible in your text editor.
这可能是因为您复制/粘贴了一些在文本编辑器中不可见的 unicode 字符。
回答by Xiangju Qin
This solution works for me on Ubuntu.
这个解决方案在 Ubuntu 上对我有用。
Add the following statement right at the beginning, before all import statements.
在开头添加以下语句,在所有导入语句之前。
# -*- coding: utf-8 -*-`
Similar approach for errors with the term '\xe2'
.
术语 错误的类似方法'\xe2'
。
回答by user2286560
Incorrect characters are probably "comma", or "quotes" on line 8. It probably occurred during the "Ctrl + C", "Ctrl + V". ;) You can try rewrite these characters manually.
不正确的字符可能是第 8 行的“逗号”或“引号”。它可能发生在“Ctrl + C”、“Ctrl + V”期间。;) 您可以尝试手动重写这些字符。