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. 6 1589
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
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.
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
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
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.
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()
.... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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>
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
| |