473,396 Members | 2,037 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,396 software developers and data experts.

Unix fold command in python

Hello Group:

Hopefully someone can answer my question.

I have a unix shell command that I would like to emulate in python. I
am sanning a file that contains a stream of data with a record size of
242 bytes but no record delimiters. There are multiple fields in each
record that can be mapped according to their position:

example

field1 byte 0-4
field2 byte 5-8

How do I make python read a record in and report the contents of a
particular field (and may be carry out an operations with that field).

Much appreciated

regards

dean
Jul 18 '05 #1
3 2279
Something like this works for me (not tested).

I'll of course this will just return the character
values of the fields. I'll leave the conversion
to int's, float's or any other value types you
many need to you. If any of the bytes contain
binary data, you will need the struct module to
decode them.

Larry Bates
Syscon, Inc.
class record:
def __init__(self, data=None):
#
# User can either pass the data at class
# creation or later by calling parse.
#
self._fields=(('field1', 0, 4),
('field2', 5, 8),
...
('fieldn',238,242))

if data is not None: self._parse(data)
return

def parse(self, data):
D=self.__dict__
for fieldname, begin, end in self._fields:
D[fieldname]=data[begin, end+1]

return

if __name__=="__main__":

#
# Open file and read all lines, if this is a huge
# file you will need to do this differently by using
# xreadlines inside the loop below.
#
fp=open(inputfile, 'r')
lines=fp.readlines()
fp.close()
#
# Create an instance of your record class
#
RECORD=record()

for line in lines:
RECORD.parse(line)
print RECORD.field1
print RECORD.field2
...
print RECORD.fieldn
"dean" <di******@uku.co.uk> wrote in message
news:70**************************@posting.google.c om...
Hello Group:

Hopefully someone can answer my question.

I have a unix shell command that I would like to emulate in python. I
am sanning a file that contains a stream of data with a record size of
242 bytes but no record delimiters. There are multiple fields in each
record that can be mapped according to their position:

example

field1 byte 0-4
field2 byte 5-8
...
...
... fieldn byte 238-242
How do I make python read a record in and report the contents of a
particular field (and may be carry out an operations with that field).

Much appreciated

regards

dean

Jul 18 '05 #2
On Mon, 14 Jun 2004 03:50:06 -0700, dean wrote:
Hello Group:

Hopefully someone can answer my question.

I have a unix shell command that I would like to emulate in python. I
am sanning a file that contains a stream of data with a record size of
242 bytes but no record delimiters. There are multiple fields in each
record that can be mapped according to their position:

example

field1 byte 0-4
field2 byte 5-8

How do I make python read a record in and report the contents of a
particular field (and may be carry out an operations with that field).

Much appreciated

regards

dean


isn't this just a slice?

like so?

alllines = file.readlines()
for each in alllines:
field1 = each[:4]
field2 = each[4:8]
Jul 18 '05 #3
On Tue, 22 Jun 2004 22:31:34 -0500, David Duncan
<dd*****@rubackedup.com> declaimed the following in comp.lang.python:
isn't this just a slice?

like so?

alllines = file.readlines()
for each in alllines:
field1 = each[:4]
field2 = each[4:8]
If the data is character strings, probably... Otherwise, I'd
suggest the struct module...

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #4

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

Similar topics

4
by: Paddy McCarthy | last post by:
This is really a comment on "Python in Systems Administration: Part I -- Better Scripting" http://www.samag.com/documents/s=8964/sam0312a/0312a.htm by Cameron Laird. Reading the article,...
3
by: Jay O'Connor | last post by:
I'm doing some Python CGI programming under Windows (Win95) but my CGIs need to run on Linux. If I try to write and save the files in IDLE, they get saved in DOS format and won't run on the Linux...
4
by: RosalieM | last post by:
I would like to understand what python needs to work on unix. And to understand how i can make it smalest possible? I dont understand at all setup. I searched in python.org and in sources but it...
3
by: Kory Wheatley | last post by:
Hi all, I've just started to learn Python and I have a question. What is the syntax to embed Unix commands, or call other programs within a Python script? for example I would like to use a...
10
by: Kotlin Sam | last post by:
For a while at least I have to work in Windows rather than UNIX, which is more familiar. I'm trying to do with Python some of the things that I've done for years in shell, in particular, sort. The...
4
by: rkoida | last post by:
Hello evryone I am a newbie to python. I have a makefile which i can compile in UNIX/LINUX, But i I am planning to write a python script which actually does what my MAKEFILE does. The make file...
23
by: Mark Dickinson | last post by:
I have a simple 192-line Python script that begins with the line: dummy0 = 47 The script runs in less than 2.5 seconds. The variable dummy0 is never referenced again, directly or indirectly,...
2
by: timdoyle05 | last post by:
Hi, I have a question relating to how Unix commands can be issued from Python programs. Im am currently writing a large test script in python and I need this script to call three other separate...
2
by: sixtyfootersdude | last post by:
Hey all! Just doing some tinkering with python. I am running mac os x and I am wondering how I can call unix commands from python. In C I think I would use a fork, although it has been...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
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 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.