Forum: PC-Programmierung Python: Seltsames Verhalten beim Schreiben von Binary


von Tim (Gast)


Lesenswert?

Hallo,
ich versuche gerade Binary-Daten in Python 2.7 zu schreiben. Dabei setze 
ich die Daten erst in einem String zusammen und wandel diese dann mit 
binascii um.
Das funktioniert auch fast immer. Aber nun bin ich ratlos.

Bei einer bestimmten String-Kombination wird 0x 0D eingefügt. Den Grund 
verstehe ich aber nicht.

Im Hex-Editor lese ich dann 7 Byte (bc 20 01 20 20 0d 0a) anstatt wie 
geplant 6 Byte. Wenn ich die Bin-Daten mit python auslese, sehe ich aber 
auch nichts von 0x 0D. Nur eben im Hex-Editor und damit vielleicht auch 
in einer anderen Software.

Was hat es damit auf sich?
1
# -*- coding: utf-8 -*-
2
import sys
3
import os.path
4
import binascii
5
6
   #h_block_sum = "012345" #working example
7
   h_test = "bc000100000abc000100000a"  # writes bc000100000d0abc000100000d0a in file ???
8
   #h_block_sum = "bc000100001abc000100000a" # writes bc000100000abc000100000d0a in file ???
9
   hb=binascii.a2b_hex(h_test)
10
   
11
   try:
12
      outputfile = open('test.txt', 'w')
13
      outputfile.write(hb)
14
   finally:
15
      outputfile.close()
16
      
17
   # test content in python
18
   inputfile = open('test.txt', 'r')
19
   block = inputfile.read(20)
20
   str1 = ""
21
   for ch in block:
22
      str1 += hex(ord(ch))+" "
23
   print("Reading Test-Data: "+str1)
24
   inputfile.close()

von joe (Gast)


Lesenswert?

Wird als Textdatei behandelt. Da wird LF automatisch CRLF.

Mach mal open( ..., "wb")); bei C geht das.

Grüße Joe

von Tom (Gast)


Lesenswert?

Dein 0x0a ist ein \n. Wenn ein Zeilenumbruch in eine Datei im Textmodus 
geöffnete Datei geschrieben wird, wird er (unter Windows) automatisch in 
\r\n (0x0d 0x0a) umgewandelt.

von c.m. (Gast)


Lesenswert?

https://www.google.de/search?q=python+file+open+binary
1
# Read the entire file as a single byte string
2
with open('somefile.bin', 'rb') as f:
3
    data = f.read()
4
5
# Write binary data to a file
6
with open('somefile.bin', 'wb') as f:
7
    f.write(b'Hello World')

eventuell ist der öffnungsmodus ("wb") die lösung, aber ich kenne python 
nicht - ist also nur ins blaue geschätzt.

von Kaj (Gast)


Lesenswert?

Wenn du eine Datei binär interpretiert möchtest, dann musst du diese 
auch binär öffnen.

Aus der Doku:

https://docs.python.org/2/library/functions.html#open
1
The default is to use text mode, which may convert '\n' characters to a
2
platform-specific representation on writing and back on reading. Thus, when
3
opening a binary file, you should append 'b' to the mode value to open the
4
file in binary mode, which will improve portability.

von Tim (Gast)


Lesenswert?

Dank an alle!
So einfach also. Hatte gar nicht mehr daran gedacht, dass der noch 
binary-Daten anfasst.

Öffnen mit 'wb' war die Lösung.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.