文字逃脱的八位字节 [英] Literal Escaped Octets

查看:94
本文介绍了文字逃脱的八位字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将原始二进制数据转换为带有转义八位字节的数据,以便将它存储在postgresql服务器上的bytea字段中。我可以在c / c ++中轻松地完成这个

,但我需要在python中完成它。我不确定如何阅读

并在python中计算一个长字符串中字节的二进制值,当它是一个非
可打印的ascii值时。我从

struct模块中读取了一些使用unpack的方法,但我真的不明白这会有什么帮助。我看了一下MIMIEncode模块,但是我不知道如何将对象

转换为字符串。是否有可以转换数据的模块?在我看来这个问题肯定已经回答了一百万次,但是我没有找到任何东西。


请参阅 http://www.postgresql.org /docs/8.1/i...pe-binary.html

描述问题域。

I am trying to convert raw binary data to data with escaped octets in
order to store it in a bytea field on postgresql server. I could do this
easily in c/c++ but I need to do it in python. I am not sure how to read
and evaluate the binary value of a byte in a long string when it is a non
printable ascii value in python. I read some ways to use unpack from the
struct module, but i really couldn''t understand where that would help. I
looked at the MIMIEncode module but I don''t know how to convert the object
to a string. Is there a module that will convert the data? It seems to me
that this question must have been answered a million times before but I
can''t find anything.

See http://www.postgresql.org/docs/8.1/i...pe-binary.html
for a description of the problem domain.

推荐答案

Chason Hayes< ch ***** @ hotmail.com>写道:

...
Chason Hayes <ch*****@hotmail.com> wrote:
...
很容易在c / c ++中,但我需要在python中完成。我不确定如何阅读
并在long字符串中计算字节的二进制值,当它在python中是一个不可打印的ascii值时。
easily in c/c++ but I need to do it in python. I am not sure how to read
and evaluate the binary value of a byte in a long string when it is a non
printable ascii value in python.




如果你有一个bytestring(AKA普通字符串)s,它的

第k个字节的二进制值是ord(s [k])。

Alex



If you have a bytestring (AKA plain string) s, the binary value of its
k-th byte is ord(s[k]).
Alex


Chason Hayes写道:
Chason Hayes wrote:
我试图将原始二进制数据转换为带有转义八位字节的数据,以便存储它在postgresql服务器上的bytea字段中。我可以在c / c ++中轻松地完成这个工作,但我需要在python中完成它。我不确定如何读取
并在long字符串中计算字节的二进制值,当它是python中不可打印的ascii值时。我从
struct模块中读取了一些使用unpack的方法,但我真的不明白它会有什么帮助。我看了MIMIEncode模块,但我不知道如何将对象转换为字符串。是否有可以转换数据的模块?在我看来,这个问题肯定已经回答了一百万次,但我找不到任何东西。

http://www.postgresql.org/docs/8.1/i...pe-binary .html
有关问题域的描述。
I am trying to convert raw binary data to data with escaped octets in
order to store it in a bytea field on postgresql server. I could do this
easily in c/c++ but I need to do it in python. I am not sure how to read
and evaluate the binary value of a byte in a long string when it is a non
printable ascii value in python. I read some ways to use unpack from the
struct module, but i really couldn''t understand where that would help. I
looked at the MIMIEncode module but I don''t know how to convert the object
to a string. Is there a module that will convert the data? It seems to me
that this question must have been answered a million times before but I
can''t find anything.

See http://www.postgresql.org/docs/8.1/i...pe-binary.html
for a description of the problem domain.



您引用的URL是讨论如何表示任意值
$字符串文字中的b $ b。如果你已经拥有Python字符串中的数据,那么

最好建议使用参数化查询 - 这样你的Python DB

API模块就可以为你逃避!


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC www.holdenweb.com

PyCon TX 2006 www.python.org/pycon/


The URL you reference is discussing how you represent arbitrary values
in string literals. If you already have the data in a Python string the
best advise is to use a parameterized query - that way your Python DB
API module will do the escaping for you!

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


2006年2月6日星期一13:39:17 +0000,Steve Holden写道:
On Mon, 06 Feb 2006 13:39:17 +0000, Steve Holden wrote:
Chason Hayes写道:
Chason Hayes wrote:
我试图将原始二进制数据转换为具有转义八位字节的数据,以便将其存储在postgresql服务器上的bytea字段中。我可以在c / c ++中轻松地完成这个工作,但我需要在python中完成它。我不确定如何读取
并在long字符串中计算字节的二进制值,当它是python中不可打印的ascii值时。我从
struct模块中读取了一些使用unpack的方法,但我真的不明白它会有什么帮助。我看了MIMIEncode模块,但我不知道如何将对象转换为字符串。是否有可以转换数据的模块?在我看来,这个问题肯定已经回答了一百万次,但我找不到任何东西。

http://www.postgresql.org/docs/8.1/i...pe-binary .html
有关问题域的描述。
I am trying to convert raw binary data to data with escaped octets in
order to store it in a bytea field on postgresql server. I could do this
easily in c/c++ but I need to do it in python. I am not sure how to read
and evaluate the binary value of a byte in a long string when it is a non
printable ascii value in python. I read some ways to use unpack from the
struct module, but i really couldn''t understand where that would help. I
looked at the MIMIEncode module but I don''t know how to convert the object
to a string. Is there a module that will convert the data? It seems to me
that this question must have been answered a million times before but I
can''t find anything.

See http://www.postgresql.org/docs/8.1/i...pe-binary.html
for a description of the problem domain.


您引用的URL正在讨论如何在字符串文字中表示任意值
。如果您已经拥有Python字符串中的数据,那么最好的建议是使用参数化查询 - 这样您的Python DB
API模块就会为您进行转义!

问候
Steve


The URL you reference is discussing how you represent arbitrary values
in string literals. If you already have the data in a Python string the
best advise is to use a parameterized query - that way your Python DB
API module will do the escaping for you!

regards
Steve




感谢您的投入。我尝试使用格式字符串和

字典,但我仍然收到数据库错误,指示非法的

字符串值。当我使用测试文件时,这个错误完全消失了

只包含文本,但每次都使用真正的二进制文件重现。

如果你能让我知道我在哪里错误或显示我的代码片段与

sql插件包含一个带有原始二进制数据的变量,可以工作,

我会非常感激。


Chason



Thanks for the input. I tried that with a format string and a
dictionary, but I still received a database error indicating illegal
string values. This error went away completely when I used a test file
consisting only of text, but reproduced everytime with a true binary file.
If you can let me know where I am wrong or show me a code snippet with a
sql insert that contains a variable with raw binary data that works,
I would greatly appreciate it.

Chason


这篇关于文字逃脱的八位字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆