所以我目前正在尝试使用服务器和一些客户端制作一个小型聊天客户端。我在网上找到了一些代码,我想用它作为我自己的代码的基础。我现在面临的问题是它是用Python 2.x编写的,而我正在使用3.x。确实没有太多需要转换的内容,但我在程序使用sys.stdin.
原始代码可以在这里找到。
这是我的代码:`
import sys, socket, select
def chat_client():
host = 'localhost'
port = 9009
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
# connect to remote host
try :
s.connect((host, port))
except :
print('Unable to connect')
sys.exit()
print('Connected to remote host. You can start sending messages')
sys.stdout.write('[Me] '); sys.stdout.flush()
while 1:
socket_list = [sys.stdin, s]
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
for sock in read_sockets:
if sock == s:
# incoming message from remote server, s …Run Code Online (Sandbox Code Playgroud) 在有人给我几十亿次这个问题的废话之前,请注意我已经在很多线程中尝试了几个答案但是它们似乎都没有适合我的问题.
import json
def parse(fn):
results = []
with open(fn) as f:
json_obj = json.loads(open(fn).read())
for r in json_obj["result"]:
print(r["name"])
parse("wine.json")
Run Code Online (Sandbox Code Playgroud)
我基本上只是打开一个json文件并迭代一些值.显然,每当我读取一个包含unicode的值时,我就会收到此错误.
Traceback (most recent call last):
File "json_test.py", line 9, in <module>
parse("wine.json")
File "json_test.py", line 7, in parse
print(r["name"])
File "C:\Python34\lib\encodings\cp850.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u201c' in position
15: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)
正如人们在其他线程中所说的那样,我试图对它进行编码等等,但无论我如何编码和/或解码,我都会遇到类似的错误.请帮忙.