473,418 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Noobie: Open file -> read characters & multiply

I've been using Python for a few days. It's such the perfect language
for parsing data!

I really like it so far, but I'm having a hard time reading a file,
reading the first few hex characters & converting them to an integer.
Once the characters are converted to an integer, I'd like to write the
data to another file.

Here's the code snipped to the bare minimum:
---
# Open File
AP_File= open("AP.txt", "r")
decoded_File= open("decoded.txt", "w")

# read & process data line by line
for line in AP_File:
time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely hosed!
decodedFile.write(time)

#close files
AP_File.close()
decoded_File.close()
---
AP.txt
000000d5 26 0600 80 00 ec 80 02 03 7d db 02 33
000000d5 26 0601 80 00 80 00 02 37 fe 54 01 09
000000d5 06 0602 80 00 e0 00 01 29 fe d2 69 99
000000d5 06 0603 80 00 e0 00 02 29 fe d2 6a 99
000000d5 26 0604 80 00 fe 54 02 09 80 00 01 5d
000000d5 06 0605 80 00 e0 00 02 15 fc 71 ca 0b
000000d5 4a 0610 81 00 86 00 02 26 12 00 02 a6
000000d5 4f 0611 00 00 00 50 00 00 00 00 07 00
000000d5 06 0612 80 00 e0 00 01 15 fc 71 c9 0b
000000d5 0a 0613 08 5c 04 88 08 98 00 00 00 00
000000d5 06 0614 80 00 e0 00 02 01 60 79 82 2b
000000d5 0a 0615 08 00 00 00 00 00 00 00 00 00
000000d5 26 0616 80 00 80 00 02 5d 04 22 3a 88
(actual files are 250MB!)

decodedTime.txt (should be)
0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33
....

My boss and I are trying to complete the same task (he figured out how
to do it, but his code uses a while != "" loop and doesn't look
pythony (it looks too 'c'). Not that there's anything wrong with that!

Any help is really appreciated.
Dec 26 '06 #1
6 1651
gonzlobo wrote:
I've been using Python for a few days. It's such the perfect language
for parsing data!

I really like it so far, but I'm having a hard time reading a file,
reading the first few hex characters & converting them to an integer.
Once the characters are converted to an integer, I'd like to write the
data to another file.

Here's the code snipped to the bare minimum:
---
# Open File
AP_File= open("AP.txt", "r")
decoded_File= open("decoded.txt", "w")

# read & process data line by line
for line in AP_File:
time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely
hosed!
decodedFile.write(time)

#close files
AP_File.close()
decoded_File.close()
---
AP.txt
000000d5 26 0600 80 00 ec 80 02 03 7d db 02 33
000000d5 26 0601 80 00 80 00 02 37 fe 54 01 09
000000d5 06 0602 80 00 e0 00 01 29 fe d2 69 99
000000d5 06 0603 80 00 e0 00 02 29 fe d2 6a 99
000000d5 26 0604 80 00 fe 54 02 09 80 00 01 5d
000000d5 06 0605 80 00 e0 00 02 15 fc 71 ca 0b
000000d5 4a 0610 81 00 86 00 02 26 12 00 02 a6
000000d5 4f 0611 00 00 00 50 00 00 00 00 07 00
000000d5 06 0612 80 00 e0 00 01 15 fc 71 c9 0b
000000d5 0a 0613 08 5c 04 88 08 98 00 00 00 00
000000d5 06 0614 80 00 e0 00 02 01 60 79 82 2b
000000d5 0a 0615 08 00 00 00 00 00 00 00 00 00
000000d5 26 0616 80 00 80 00 02 5d 04 22 3a 88
(actual files are 250MB!)

decodedTime.txt (should be)
0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33
...

My boss and I are trying to complete the same task (he figured out how
to do it, but his code uses a while != "" loop and doesn't look
pythony (it looks too 'c'). Not that there's anything wrong with that!

Any help is really appreciated.
for line in AP_file:
print >>decoded_File, '%s.%04d' % divmod(int(line[:8], 16), 10000
), line[9:].rstrip()

or:

for line in AP_file:
print >>decoded_File, '%.4f' % (int(line[:8], 16) * .0001
), line[9:].rstrip()
--Scott David Daniels
sc***********@acm.org
Dec 27 '06 #2
On Tue, 26 Dec 2006 16:50:06 -0700, gonzlobo wrote:
I've been using Python for a few days. It's such the perfect language
for parsing data!

I really like it so far, but I'm having a hard time reading a file,
reading the first few hex characters & converting them to an integer.
Once the characters are converted to an integer, I'd like to write the
data to another file.

Here's the code snipped to the bare minimum:
---
# Open File
AP_File= open("AP.txt", "r")
decoded_File= open("decoded.txt", "w")

# read & process data line by line
for line in AP_File:
time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely hosed!
decodedFile.write(time)
What does "this line is completely hosed!" mean? Does it crash your PC?
Does it raise an exception? Do the wrong thing?

Try this:

for line in AP_File:
# Typical line looks like this:
# 000000d5 26 0600 80 00 ec 80 02 03 7d db 02 33
# This should convert to:
# 0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33
time = int(line[0:8], 16) * 0.0001
line = str(time) + line[8:]
decodedFile.write(line)

You might need to think about how many decimal places you want the time to
store.

My boss and I are trying to complete the same task (he figured out how
to do it, but his code uses a while != "" loop and doesn't look
pythony (it looks too 'c'). Not that there's anything wrong with that!


You can convert this:

AP_File= file("AP.txt", "r") # file is recommended over "open"
line = AP_File.readline()
while line != "":
do_something_with_line
line = AP_File.readline()
AP_File.close()
into this:

AP_File= file("AP.txt", "r")
for line in AP_File:
do_something_with_line
AP_File.close()

You need to look at what your boss does with the lines, not how he does
the loop.
--
Steven.

Dec 27 '06 #3

gonzlobo wrote:
I've been using Python for a few days. It's such the perfect language
for parsing data!

I really like it so far, but I'm having a hard time reading a file,
reading the first few hex characters & converting them to an integer.
Once the characters are converted to an integer, I'd like to write the
data to another file.

Here's the code snipped to the bare minimum:
---
# Open File
AP_File= open("AP.txt", "r")
decoded_File= open("decoded.txt", "w")

# read & process data line by line
for line in AP_File:
time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely hosed!
decodedFile.write(time)

#close files
AP_File.close()
decoded_File.close()
---
AP.txt
000000d5 26 0600 80 00 ec 80 02 03 7d db 02 33
000000d5 26 0601 80 00 80 00 02 37 fe 54 01 09
000000d5 06 0602 80 00 e0 00 01 29 fe d2 69 99
000000d5 06 0603 80 00 e0 00 02 29 fe d2 6a 99
000000d5 26 0604 80 00 fe 54 02 09 80 00 01 5d
000000d5 06 0605 80 00 e0 00 02 15 fc 71 ca 0b
000000d5 4a 0610 81 00 86 00 02 26 12 00 02 a6
000000d5 4f 0611 00 00 00 50 00 00 00 00 07 00
000000d5 06 0612 80 00 e0 00 01 15 fc 71 c9 0b
000000d5 0a 0613 08 5c 04 88 08 98 00 00 00 00
000000d5 06 0614 80 00 e0 00 02 01 60 79 82 2b
000000d5 0a 0615 08 00 00 00 00 00 00 00 00 00
000000d5 26 0616 80 00 80 00 02 5d 04 22 3a 88
(actual files are 250MB!)

decodedTime.txt (should be)
0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33
...

My boss and I are trying to complete the same task (he figured out how
to do it, but his code uses a while != "" loop and doesn't look
pythony (it looks too 'c'). Not that there's anything wrong with that!

Any help is really appreciated.
Use the built-in int(). It has an optional argument "radix" which
specifies the base for the conversion. For example:
>>int("0x0A", 16)
10
Dec 27 '06 #4

WaterWalk wrote:
gonzlobo wrote:
I've been using Python for a few days. It's such the perfect language
for parsing data!

I really like it so far, but I'm having a hard time reading a file,
reading the first few hex characters & converting them to an integer.
Once the characters are converted to an integer, I'd like to write the
data to another file.

Here's the code snipped to the bare minimum:
---
# Open File
AP_File= open("AP.txt", "r")
decoded_File= open("decoded.txt", "w")

# read & process data line by line
for line in AP_File:
time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely hosed!
decodedFile.write(time)

#close files
AP_File.close()
decoded_File.close()
---
AP.txt
000000d5 26 0600 80 00 ec 80 02 03 7d db 02 33
000000d5 26 0601 80 00 80 00 02 37 fe 54 01 09
000000d5 06 0602 80 00 e0 00 01 29 fe d2 69 99
000000d5 06 0603 80 00 e0 00 02 29 fe d2 6a 99
000000d5 26 0604 80 00 fe 54 02 09 80 00 01 5d
000000d5 06 0605 80 00 e0 00 02 15 fc 71 ca 0b
000000d5 4a 0610 81 00 86 00 02 26 12 00 02 a6
000000d5 4f 0611 00 00 00 50 00 00 00 00 07 00
000000d5 06 0612 80 00 e0 00 01 15 fc 71 c9 0b
000000d5 0a 0613 08 5c 04 88 08 98 00 00 00 00
000000d5 06 0614 80 00 e0 00 02 01 60 79 82 2b
000000d5 0a 0615 08 00 00 00 00 00 00 00 00 00
000000d5 26 0616 80 00 80 00 02 5d 04 22 3a 88
(actual files are 250MB!)

decodedTime.txt (should be)
0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33
...

My boss and I are trying to complete the same task (he figured out how
to do it, but his code uses a while != "" loop and doesn't look
pythony (it looks too 'c'). Not that there's anything wrong with that!

Any help is really appreciated.

Use the built-in int(). It has an optional argument "radix" which
specifies the base for the conversion. For example:
>int("0x0A", 16)
10
Oh I forget that ">>>" will cause the line to be hidden by default. The
example is:
int("0x0A", 16) # will return 10

Dec 27 '06 #5

WaterWalk wrote:
WaterWalk wrote:
gonzlobo wrote:
I've been using Python for a few days. It's such the perfect language
for parsing data!
>
I really like it so far, but I'm having a hard time reading a file,
reading the first few hex characters & converting them to an integer.
Once the characters are converted to an integer, I'd like to write the
data to another file.
>
Here's the code snipped to the bare minimum:
---
# Open File
AP_File= open("AP.txt", "r")
decoded_File= open("decoded.txt", "w")
>
# read & process data line by line
for line in AP_File:
time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely hosed!
decodedFile.write(time)
>
#close files
AP_File.close()
decoded_File.close()
---
AP.txt
000000d5 26 0600 80 00 ec 80 02 03 7d db 02 33
000000d5 26 0601 80 00 80 00 02 37 fe 54 01 09
000000d5 06 0602 80 00 e0 00 01 29 fe d2 69 99
000000d5 06 0603 80 00 e0 00 02 29 fe d2 6a 99
000000d5 26 0604 80 00 fe 54 02 09 80 00 01 5d
000000d5 06 0605 80 00 e0 00 02 15 fc 71 ca 0b
000000d5 4a 0610 81 00 86 00 02 26 12 00 02 a6
000000d5 4f 0611 00 00 00 50 00 00 00 00 07 00
000000d5 06 0612 80 00 e0 00 01 15 fc 71 c9 0b
000000d5 0a 0613 08 5c 04 88 08 98 00 00 00 00
000000d5 06 0614 80 00 e0 00 02 01 60 79 82 2b
000000d5 0a 0615 08 00 00 00 00 00 00 00 00 00
000000d5 26 0616 80 00 80 00 02 5d 04 22 3a 88
(actual files are 250MB!)
>
decodedTime.txt (should be)
0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33
...
>
My boss and I are trying to complete the same task (he figured out how
to do it, but his code uses a while != "" loop and doesn't look
pythony (it looks too 'c'). Not that there's anything wrong with that!
>
Any help is really appreciated.
Use the built-in int(). It has an optional argument "radix" which
specifies the base for the conversion. For example:
>>int("0x0A", 16)
>>10

Oh I forget that ">>>" will cause the line to be hidden by default. The
example is:
int("0x0A", 16) # will return 10
I misunderstand the question, sorry for this.

Why not just split the line read since each number is separated by
space or tab. After splitting there is a list of numbers, then convert
the first element and write the list into a file.

Dec 27 '06 #6
Thanks to all that responded. I chose a modified version of Scott's
second recommendation:

time = line[:8]
decoded_File.write( '%00.4f' % (int(time, 16) * .0001) + ', ')

'print >>' added a CRLF that I didn't need, so I went with '.print' (I
need to process about 20 values from the remaining bytes).

Thank you.

On 12/26/06, Scott David Daniels <sc***********@acm.orgwrote:
....
Any help is really appreciated.
for line in AP_file:
print >>decoded_File, '%s.%04d' % divmod(int(line[:8], 16), 10000
), line[9:].rstrip()

or:

for line in AP_file:
print >>decoded_File, '%.4f' % (int(line[:8], 16) * .0001
), line[9:].rstrip()
....
Dec 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mcc | last post by:
When i put these two java scripts in the same html file only the fade script works. Why is that??? this part is in the <head>
2
by: osgnamah | last post by:
Hi All; I am getting ready to launch a shareware program in the next few weeks and the last step is going to be creating a website. So the last few days I've been doing some web surfing in...
11
by: (Pete Cresswell) | last post by:
I'm about to start climbing the .Net learning curve. Would like to start by reproducing a little application that I already have up and running using an MS Access front end. It manages...
6
by: Aristotelis E. Charalampakis | last post by:
Hi all, this is a newbie question :-) I was wondering if there was a way to use the switch statement in a manner that each case statement includes more that a simple value. i.e.: switch (...
2
by: megafrenzy | last post by:
I've have an idea for a personal project. I want to access my car's OBD-II port via serial to query engine paramters and represent them on my PC monitor. The various methods to represent them would...
12
by: Capt_Ron | last post by:
Sorry to have to ask this but... I have a form that asks for SQL connection information. I want to close the form when I click Save and then open the login form. I can Hide the form but it...
4
by: ProvoWallis | last post by:
Hi, I'm trying to write a script that will create a new directory and then write the results to this newly created directory but it doesn't seem to work for me and I don't know why. I'm hoping...
3
by: alcapoontje | last post by:
hi there, i'm already searching the internet for a couple of days to find an answer on this (simple) questions but i couln't find anything usefull. - how to quit and open a programm like "word" with...
1
by: jobs | last post by:
I've got a class with a new constructor Sub New(ByVal connstring As String) p_cnn = New SqlConnection(connstring) p_cnn.Open() End Sub I instatiate it like this in my ssis script task vb.net...
1
by: nick777 | last post by:
Hope the Community can bear with me as I muddle with the vocabulary since I am not really sure if I am going about this the correct way. My question is as follows: If I had some sample data in...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.