Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98667 > unrolled thread
| Started by | fl <rxjwg98@gmail.com> |
|---|---|
| First post | 2015-11-11 19:04 -0800 |
| Last post | 2015-11-11 20:34 -0700 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
How to get 'od' run? fl <rxjwg98@gmail.com> - 2015-11-11 19:04 -0800
Re: How to get 'od' run? Michael Torrie <torriem@gmail.com> - 2015-11-11 20:21 -0700
Re: How to get 'od' run? Michael Torrie <torriem@gmail.com> - 2015-11-11 20:34 -0700
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-11-11 19:04 -0800 |
| Subject | How to get 'od' run? |
| Message-ID | <24ed2ddb-aaea-455e-bf45-10e1cd8e8376@googlegroups.com> |
Hi,
I am learning python. I see a previous post has such code:
>>> data = '"binääridataa"\n'.encode('utf-8')
>>> f = open('roska.txt', 'wb')
>>> f.write(data)
17
>>> f.close()
The .encode methods produced a bytestring, which Python likes to display
as ASCII characters where it can and in hexadecimal where it cannot:
>>> data
b'"bin\xc3\xa4\xc3\xa4ridataa"\n'
An "octal dump" in characters (where ASCII, otherwise apparently octal)
and the corresponding hexadecimal shows that it is, indeed, these bytes
that ended up in the file:
$ od -t cx1 roska.txt
When I run the above line with python 2.7, it does not recognize 'od'.
Is it from a package? Or some internal function?
Thanks,
[toc] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2015-11-11 20:21 -0700 |
| Message-ID | <mailman.253.1447298480.16136.python-list@python.org> |
| In reply to | #98667 |
On 11/11/2015 08:04 PM, fl wrote:
> Hi,
>
> I am learning python. I see a previous post has such code:
>
>
>
>
>
> >>> data = '"binääridataa"\n'.encode('utf-8')
> >>> f = open('roska.txt', 'wb')
> >>> f.write(data)
> 17
> >>> f.close()
>
> The .encode methods produced a bytestring, which Python likes to display
> as ASCII characters where it can and in hexadecimal where it cannot:
>
> >>> data
> b'"bin\xc3\xa4\xc3\xa4ridataa"\n'
>
> An "octal dump" in characters (where ASCII, otherwise apparently octal)
> and the corresponding hexadecimal shows that it is, indeed, these bytes
> that ended up in the file:
>
> $ od -t cx1 roska.txt
^^^
This is most likely a bash prompt. Therefore "od" is a program on your
computer. Nothing to do with Python at all.
To get Python to display \x## hex codes for non-ascii characters in a
byte stream, you can print out the repr() of the byte string. For example:
print (repr(my_unicode_string.encode('utf-8')))
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2015-11-11 20:34 -0700 |
| Message-ID | <mailman.254.1447299295.16136.python-list@python.org> |
| In reply to | #98667 |
On 11/11/2015 08:21 PM, Michael Torrie wrote:
> On 11/11/2015 08:04 PM, fl wrote:
>> Hi,
>>
>> I am learning python. I see a previous post has such code:
>>
>>
>>
>>
>>
>> >>> data = '"binääridataa"\n'.encode('utf-8')
>> >>> f = open('roska.txt', 'wb')
>> >>> f.write(data)
>> 17
>> >>> f.close()
>>
>> The .encode methods produced a bytestring, which Python likes to display
>> as ASCII characters where it can and in hexadecimal where it cannot:
>>
>> >>> data
>> b'"bin\xc3\xa4\xc3\xa4ridataa"\n'
>>
>> An "octal dump" in characters (where ASCII, otherwise apparently octal)
>> and the corresponding hexadecimal shows that it is, indeed, these bytes
>> that ended up in the file:
>>
>> $ od -t cx1 roska.txt
> ^^^
> This is most likely a bash prompt. Therefore "od" is a program on your
> computer. Nothing to do with Python at all.
>
> To get Python to display \x## hex codes for non-ascii characters in a
> byte stream, you can print out the repr() of the byte string. For example:
>
> print (repr(my_unicode_string.encode('utf-8')))
Also there are numerous recipes for doing standard hex dumps out there.
For example,
http://code.activestate.com/recipes/142812-hex-dumper/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web