似乎有两种不同的方法将字符串转换为字节,如TypeError的答案所示:'str'不支持缓冲区接口
哪种方法更好或更好Pythonic?或者只是个人喜好?
b = bytes(mystring, 'utf-8')
b = mystring.encode('utf-8')
Run Code Online (Sandbox Code Playgroud) 我想将我运行的命令的输出分配给os.system变量,并防止它输出到屏幕.但是,在下面的代码中,输出被发送到屏幕并且打印的var值为0,我猜这表示命令是否成功运行.有没有办法将命令输出分配给变量并阻止它显示在屏幕上?
var = os.system("cat /etc/services")
print var #Prints 0
Run Code Online (Sandbox Code Playgroud) 例如,我有一个像这样的字符串(返回值subprocess.check_output):
>>> b'a string'
b'a string'
Run Code Online (Sandbox Code Playgroud)
无论我做了什么,它总是印b'在字符串之前的烦人:
>>> print(b'a string')
b'a string'
>>> print(str(b'a string'))
b'a string'
Run Code Online (Sandbox Code Playgroud)
有没有人有任何关于如何将其用作普通字符串或将其转换为普通字符串的想法?
我已阅读XML电子邮件附件
bytes_string=part.get_payload(decode=False)
Run Code Online (Sandbox Code Playgroud)
有效负载以字节字符串形式出现,正如我的变量名所示.
我试图使用推荐的Python 3方法将此字符串转换为可以操作的可用字符串.
该示例显示:
Run Code Online (Sandbox Code Playgroud)str(b'abc','utf-8')
如何将b(bytes)关键字参数应用于我的变量bytes_string并使用推荐的方法?
我尝试的方式不起作用:
str(bbytes_string, 'utf-8')
Run Code Online (Sandbox Code Playgroud) 我发现使用Python读取二进制文件特别困难.你能帮我个忙吗?我需要阅读这个文件,它在Fortran 90中很容易阅读
int*4 n_particles, n_groups
real*4 group_id(n_particles)
read (*) n_particles, n_groups
read (*) (group_id(j),j=1,n_particles)
Run Code Online (Sandbox Code Playgroud)
具体来说,文件格式为:
Bytes 1-4 -- The integer 8.
Bytes 5-8 -- The number of particles, N.
Bytes 9-12 -- The number of groups.
Bytes 13-16 -- The integer 8.
Bytes 17-20 -- The integer 4*N.
Next many bytes -- The group ID numbers for all the particles.
Last 4 bytes -- The integer 4*N.
Run Code Online (Sandbox Code Playgroud)
我怎么用Python阅读?我尝试了一切,但从未奏效.我有没有机会在python中使用f90程序,读取这个二进制文件,然后保存我需要使用的数据?
我正在尝试学习如何从页面自动获取网址.在下面的代码中,我试图获取网页的标题:
import urllib.request
import re
url = "http://www.google.com"
regex = r'<title>(,+?)</title>'
pattern = re.compile(regex)
with urllib.request.urlopen(url) as response:
html = response.read()
title = re.findall(pattern, html)
print(title)
Run Code Online (Sandbox Code Playgroud)
我得到了这个意想不到的错误:
Traceback (most recent call last):
File "path\to\file\Crawler.py", line 11, in <module>
title = re.findall(pattern, html)
File "C:\Python33\lib\re.py", line 201, in findall
return _compile(pattern, flags).findall(string)
TypeError: can't use a string pattern on a bytes-like object
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我想将一个字节数组转换为JSON格式,这是我的源代码:
my_bytes_value = b'[{\'Date\': \'2016-05-21T21:35:40Z\', \'CreationDate\': \'2012-05-05\', \'LogoType\': \'png\', \'Ref\': 164611595, \'Classe\': [\'Email addresses\', \'Passwords\'],\'Link\':\'http://some_link.com\'}]'
Run Code Online (Sandbox Code Playgroud)
这是我想要的结果:
[{
"Date": "2016-05-21T21:35:40Z",
"CreationDate": "2012-05-05",
"LogoType": "png",
"Ref": 164611595,
"Classes": [
"Email addresses",
"Passwords"
],
"Link": "http://some_link.com"}]
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
编辑:
首先,我将字节转换为字符串:
my_new_string_value = my_bytes_value.decode("utf-8")
Run Code Online (Sandbox Code Playgroud)
但是当我尝试加载到JSON时:my_json = json.loads(my_new_string_value)
我收到此错误:json.decoder.JSONDecodeError:期望值:第1行第174列(char 173)
我有一个整数ascii值列表,我需要将其转换为字符串(二进制)以用作加密操作的键.(我在python中重新实现java加密代码)
这有效(假设一个8字节的密钥):
key = struct.pack('BBBBBBBB', 17, 24, 121, 1, 12, 222, 34, 76)
Run Code Online (Sandbox Code Playgroud)
但是,我宁愿没有密钥长度和硬盘编码的unpack()参数列表.
在给定初始的整数列表的情况下,如何正确实现?
谢谢!
我有一个代码,这样:
a = "\u0432"
b = u"\u0432"
c = b"\u0432"
d = c.decode('utf8')
print(type(a), a)
print(type(b), b)
print(type(c), c)
print(type(d), d)
Run Code Online (Sandbox Code Playgroud)
并输出:
<class 'str'> ?
<class 'str'> ?
<class 'bytes'> b'\\u0432'
<class 'str'> \u0432
Run Code Online (Sandbox Code Playgroud)
为什么在后一种情况下我看到的是字符代码,而不是字符?我如何将Byte字符串转换为Unicode字符串,在输出的情况下,我看到了字符而不是代码?
我正在寻找从python调用shell脚本的方法,并使用日志记录将他们的stdout和stderr写入文件.这是我的代码:
import logging
import tempfile
import shlex
import os
def run_shell_command(command_line):
command_line_args = shlex.split(command_line)
logging.info('Subprocess: \"' + command_line + '\"')
process_succeeded = True
try:
process_output_filename = tempfile.mktemp(suffix = 'subprocess_tmp_file_')
process_output = open(process_output_filename, 'w')
command_line_process = subprocess.Popen(command_line_args,\
stdout = process_output,\
stderr = process_output)
command_line_process.wait()
process_output.close()
process_output = open(process_output_filename, 'r')
log_subprocess_output(process_output)
process_output.close()
os.remove(process_output_filename)
except:
exception = sys.exc_info()[1]
logging.info('Exception occured: ' + str(exception))
process_succeeded = False
if process_succeeded:
logging.info('Subprocess finished')
else:
logging.info('Subprocess failed')
return process_succeeded
Run Code Online (Sandbox Code Playgroud)
我确信无需创建临时文件来存储流程输出就可以实现.有任何想法吗?
python ×9
python-3.x ×5
string ×4
binary ×2
bytestring ×1
fortran ×1
json ×1
logging ×1
subprocess ×1
unicode ×1
web-crawler ×1