有没有办法将字符串从大写,甚至部分大写转换为小写?
例如公里 - >公里.
例如,如果我有一个unicode字符串,我可以将其编码为ASCII字符串,如下所示:
>>> u'\u003cfoo/\u003e'.encode('ascii')
'<foo/>'
Run Code Online (Sandbox Code Playgroud)
但是,我有这个ASCII字符串:
'\u003foo\u003e'
Run Code Online (Sandbox Code Playgroud)
...我想变成与上面第一个例子中相同的ASCII字符串:
'<foo/>'
Run Code Online (Sandbox Code Playgroud) 我在python 3.3.4中使用"decode"方法遇到了一些问题.这是我的代码:
for lines in open('file','r'):
decodedLine = lines.decode('ISO-8859-1')
line = decodedLine.split('\t')
Run Code Online (Sandbox Code Playgroud)
但是我无法解决这个问题:
AttributeError: 'str' object has no attribute 'decode'
Run Code Online (Sandbox Code Playgroud)
你有什么想法?谢谢
我正在开发一个 Python 项目,该项目允许与日志进行基本的 TCP 通信。但是正在接收的数据是使用 ab 前缀接收的。
在对其他错误进行了数小时的故障排除后,我归结为这个总是发生的最后一个错误,我似乎无法修复。经过几天的研究,我发现 b 前缀是数据字符串的一部分,每当我尝试使用 print(data.decode()) 或 print(data.decode('utf-8') 我得到错误:
2018-06-21 21:17:38,801 STCP STOPPED DUE TO ERROR ON main.py, main()
Traceback (most recent call last):
File "main.py", line 14, in main
receiver.receive()
File "D:\SecureNetworks\SecureTCP\receiver.py", line 26, in receive
print(data.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'
Run Code Online (Sandbox Code Playgroud)
服务器在 Python 3.6 上运行,客户端在 Python 2.7 上运行
这是我的服务器代码:
主文件
import logging
import receiver
import config
logger = logging.getLogger(config.log)
hdlr = logging.FileHandler(str(config.log) + '.log')
formatter = logging.Formatter('%(asctime)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG) …Run Code Online (Sandbox Code Playgroud)