I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.
for name in os.listdir(DOWNLOAD_DIR):
filename = s.path.join(DOWNLOAD_DIR, name)
if os.path.isfile(filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.
Any quick tips?
thanks 5 2118
On 2007-02-06, jeff <je**@kalikstein.comwrote:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.
for name in os.listdir(DOWNLOAD_DIR):
filename = s.path.join(DOWNLOAD_DIR, name)
if os.path.isfile(filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.
Any quick tips?
What are the f.read(2) calls returning?
--
Grant Edwards grante Yow! Hello, GORRY-O!! I'm
at a GENIUS from HARVARD!!
visi.com
En Tue, 06 Feb 2007 19:01:20 -0300, jeff <je**@kalikstein.comescribió:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.
for name in os.listdir(DOWNLOAD_DIR):
filename = s.path.join(DOWNLOAD_DIR, name)
if os.path.isfile(filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.
Looks fine to me...
--
Gabriel Genellina
On Feb 6, 4:01 pm, "jeff" <j...@kalikstein.comwrote:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.
for name in os.listdir(DOWNLOAD_DIR):
filename = s.path.join(DOWNLOAD_DIR, name)
if os.path.isfile(filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.
Any quick tips?
thanks
Wow, supreme stupidity on my part. It turns out that there were a lot
of zeros at the beginning of the file, and the slowness of the console
just showed me the zero data during the test time of ~ 10 seconds. If
I throw away the zeros, I see my real data....sorry for the time waste
On Feb 7, 9:01 am, "jeff" <j...@kalikstein.comwrote:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.
for name in os.listdir(DOWNLOAD_DIR):
filename = s.path.join(DOWNLOAD_DIR, name)
if os.path.isfile(filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.
Any quick tips?
Looks to me like it should work -- at least until it hits the end of
the first file. What do you expect to happen at the end of the first
file?? Or did you so snippetise the code so that even if the missing
import statements are added back, it still won't get into the 2nd
file?
I suggest a few more print statements, so that you can see what is
happening.
Try something like this (untested):
f = open(filename, 'rb')
print "Opened", filename
while True:
buff = f.read(2)
if not buff: break # EOF
if len(buff) == 1:
print repr(buff), ord(buff)
break
ele = unpack('<h', buff)[0]
print repr(buff), ele
Print this, cut it out, and paste it to the inside of your hat:
*repr() is your friend*
HTH,
John
On Feb 7, 9:34 am, "jeff" <j...@kalikstein.comwrote:
On Feb 6, 4:01 pm, "jeff" <j...@kalikstein.comwrote:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.
for name in os.listdir(DOWNLOAD_DIR):
filename = s.path.join(DOWNLOAD_DIR, name)
if os.path.isfile(filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.
Any quick tips?
thanks
Wow, supreme stupidity on my part. It turns out that there were a lot
of zeros at the beginning of the file, and the slowness of the console
just showed me the zero data during the test time of ~ 10 seconds.
That's a rather severe case of premature emailisation :-)
If
I throw away the zeros, I see my real data....sorry for the time waste
Some further suggestions (not mutually exclusive):
1. redirect the console to a file
2. write more per line
3. download a free or shareware gadget that will show you the contents
of a file in hex and char
4. You may want/need to write yourself a better dumper that's tailored
to the type of files that you are loooking at, e.g.
498: 0031 FONT len = 001e (30)
502: b4 00 00 00 08 00 90 01 00 00 00 00 00 a5 07 01 ?~~~?~??
~~~~~???
518: 56 00 65 00 72 00 64 00 61 00 6e 00 61 00
V~e~r~d~a~n~a~
532: 041e FORMAT len = 001e (30)
536: 05 00 19 00 00 23 2c 23 23 30 5c 20 22 44 4d 22 ?~?
~~#,##0\ "DM"
552: 3b 5c 2d 23 2c 23 23 30 5c 20 22 44 4d 22 ;\-#,##0\
"DM"
566: 041e FORMAT len = 0023 (35)
570: 06 00 1e 00 00 23 2c 23 23 30 5c 20 22 44 4d 22 ?~?
~~#,##0\ "DM"
586: 3b 5b 52 65 64 5d 5c 2d 23 2c 23 23 30 5c 20 22 ;[Red]\-
#,##0\ "
602: 44 4d 22 DM"
HTH,
John This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Olivier Maurice |
last post by:
Hi all,
I suppose some of you know the program Redmon (type redmon in google, first
result). This neat little tool allows to hook up any functionality to a
printer by putting the file printed...
|
by: Brian |
last post by:
Hello,
I have a text file I'm attempting to parse. There are about 50 fixed width
fields in each line / row. For example (shortened for brevity):
W1234Somebody East 101110001111010101...
|
by: abmd.tw |
last post by:
dear all :
please help me to slove the problem
as below code , it can't run .
i want to read a file and display.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <assert.h>
|
by: Arnold |
last post by:
I need to read a binary file and store it into a buffer in memory (system
has large amount of RAM, 2GB+) then pass it to a function. The function
accepts input as 32 bit unsigned longs (DWORD). I...
|
by: Michael Mair |
last post by:
Cheerio,
I would appreciate opinions on the following:
Given the task to read a _complete_ text file into a string:
What is the "best" way to do it?
Handling the buffer is not the problem...
|
by: Jón Sveinsson |
last post by:
Hello everyone
I have been able to read data from binary files to
filestrean, the data in the files are structured, what
I'm trying to do is to loop through the binary files and
add data to my...
|
by: Niyazi |
last post by:
Hi,
I am developing small insurance application using VB.NET and SQL server 2000.
My tables in SQL server are:
tbl_Customer (stores the custmer information)
tbl_CustImage ...
|
by: EP |
last post by:
This inquiry may either turn out to be about the suitability of the
SHA-1 (160 bit digest) for file identification, the sha function in
Python ... or about some error in my script. Any insight...
|
by: arne.muller |
last post by:
Hello,
I've come across some problems reading strucutres from binary files.
Basically I've some strutures
typedef struct {
int i;
double x;
int n;
double *mz;
|
by: Sam |
last post by:
Hi,
I have one table like :
MyTable {field1, field2, startdate, enddate}
I want to have the count of field1 between startdate and enddate, and
the count of field2 where field2 = 1 between...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |