相关疑难解决方法(0)

如何在Python中小写一个字符串?

有没有办法将字符串从大写,甚至部分大写转换为小写?

例如公里 - >公里.

python string lowercase uppercase

1908
推荐指数
5
解决办法
227万
查看次数

如何将ASCII字符串视为unicode并在python中对其中的转义字符进行转换?

例如,如果我有一个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 unicode ascii

26
推荐指数
1
解决办法
2万
查看次数

'str'对象在Python3中没有属性'decode'

我在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 python-3.x python-3.3

9
推荐指数
3
解决办法
5万
查看次数

Python AttributeError:“str”对象没有“decode”属性

我正在开发一个 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)

python

2
推荐指数
1
解决办法
3万
查看次数

标签 统计

python ×4

ascii ×1

lowercase ×1

python-3.3 ×1

python-3.x ×1

string ×1

unicode ×1

uppercase ×1