473,698 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2296
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,2 42))

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

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

return

if __name__=="__ma in__":

#
# 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(inputfi le, 'r')
lines=fp.readli nes()
fp.close()
#
# Create an instance of your record class
#
RECORD=record()

for line in lines:
RECORD.parse(li ne)
print RECORD.field1
print RECORD.field2
...
print RECORD.fieldn
"dean" <di******@uku.c o.uk> wrote in message
news:70******** *************** ***@posting.goo gle.com...
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*****@ruback edup.com> declaimed the following in comp.lang.pytho n:
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.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Jul 18 '05 #4

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

Similar topics

4
1675
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, although I am a fan of Python, I kinda take issue with the example given. It doesn't seem to fit with the concept of 'many smaller utilities doing one thing well, connected via pipes' If I had the task of writing a Unix utility to match the GUI Python
3
2597
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 server. If I load the files into another text editor and explictely "Save As" in UNIX format, they work fine, but the other editor is not Python aware so I'd rather not have to use if for main development, and having to load and resave my scripts...
4
1768
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 is not clear at all for me. Can python run with a posix C compiler and minix or something like that ? Thanks
3
2260
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 system chmod unix command in the python script:
10
1967
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 shell sort is pretty easy to use: % sort -t, +2 +5 imputfilename <return> where -t is the field separator, in this case a comma, , and +2 and +4 are the fields to be sorted, in that order. Actually, the fields are zero-based, so the first and...
4
4057
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 is #Makefile and some scripts to give output #numbers #Change till sign #END
23
2631
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, by the rest of the script. Here's the surprise: if I remove or comment out this first line, the script takes more than 15 seconds to run. So it appears that adding a redundant line produces a spectacular six-fold increase in speed!
2
3000
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 Python scripts, where each one executes in it own thread of control. I would like to use a Unix command to get each script to run in its own shell. I have tried using the "Commands" module but after I issue the first "command", execution blocks...
2
2315
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 sometime since I have used C so I am a little fuzzy on the syntax. I also want to use what the unix command returns. Example: Typed in the command line: python myProgram.py
0
8678
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9166
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.