相关疑难解决方法(0)

TypeError:write()参数必须是str而不是byte,升级到python 3

我正在尝试将代码升级到python3。这行遇到了一些麻烦,

output_file = open(working_dir + "E"+str(s)+".txt", "w+")

output_file.write(','.join(headers) + "\n")
Run Code Online (Sandbox Code Playgroud)

并得到这个错误 TypeError: sequence item 0: expected str instance, bytes found

我尝试过的

  output_file.write(b",".join(headers) + b"\n")

TypeError: write() argument must be str, not bytes
Run Code Online (Sandbox Code Playgroud)

香港专业教育学院也尝试decode()在联接上使用,也尝试使用rw+b打开。

如何str在python 3中转换为?

python python-3.x

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

如何在Python 3中将字节转换为字符串

我想编写一个返回字符串而不是字节的函数。
功能:

def read_image(path):
    with open(path, "rb") as f:
        data = f.read()
    return data
image_data = read_image("/home/user/icon.jpg")
Run Code Online (Sandbox Code Playgroud)

如何将值转换image_data为类型str。如果成功转换为字符串,如何将字符串转换为字节。

python python-3.x

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

如何将bytes类型转换为str或json?

如何将 bytes 类型数组转换为 str 或 json?我有这个 python 字节码,我需要转换为 json 格式或字符串格式。我怎样才能做到这一点?

b'x\xda\x04\xc0\xb1\r\xc4 \x0c\x85\xe1]\xfe\x9a\x06\xae\xf36\'B\x11\xc9J$?\xbbB\xec\x9eo\xb3"\xde\xc0\x9ero\xc4Ryb\x1b\xe5?K\x18\xaa9\x97\xc4i\xdc\x17\xd6\xc7\xaf\x8f\xf3\x05\x00\x00\xff\xff l\x12l'
Run Code Online (Sandbox Code Playgroud)

python arrays json encode decode

1
推荐指数
1
解决办法
6898
查看次数

无法删除python3字符串中的\n和\ t?

所以我一直试图从CL格式化所拍摄的网页,所以我可以将它发送到我的电子邮件,但这是我每次尝试删除\n\t

b'\n\n\n\t\n\t\n\t\n\t\n\t\n\t\n\n\n\n\t\n\n\n\t
\n\t\t\t
\n\t
\n\t\t
\n\t\t\t
\n 0 favorites\n
\n\n\t\t
\n\t\t
?
\n\t\t
?
\n\t\t
\n \n
\n
\n\t \tCL wenatchee all personals casual encounters\n
\n
\n\t\t
\n\t
\n
\n\n\t\t
\n\t\t\t
\n\t\n\t\t\n\t\n\n\n\nReply to: 59nv6-4031116628@pers.craigslist.org\n
\n\n\n\t
\n\t\n\t\tflag [?] :\n\t\t\n\t\t\tmiscategorized\n\t\t\n\t\t\tprohibited\n\t\t\n\t\t\tspam\n\t\t\n\t\t\tbest of\n\t\n
\n\n\t\t

Posted: 2013-08-28, 8:23AM PDT
\n
\n\n
\n \n Well... - w4m - 22 (Wenatchee)\n
Run Code Online (Sandbox Code Playgroud)

我已经尝试过剥离,替换甚至正则表达但没有任何目击它,它总是出现在我的电子邮件中,不受任何影响.

这是代码:

try:
    if url.find('http://') == -1:
        url = 'http://wenatchee.craigslist.org' + url
    html = urlopen(url).read()
    html = str(html)
    html = re.sub('\s+',' ', html) …
Run Code Online (Sandbox Code Playgroud)

python-3.x

0
推荐指数
1
解决办法
1649
查看次数

标签 统计

python ×3

python-3.x ×3

arrays ×1

decode ×1

encode ×1

json ×1