Hi,
I know that i can do readline() from a file object.
However, how can I read till a specific seperator?
for exmple,
if my files are
name
profession
id
#
name2
profession3
id2
I would like to read this file as a record.
I can do this in perl by defining a record seperator;
is there an equivalent in python?
thanks 35 2950 le*******@yahoo .com wrote: I know that i can do readline() from a file object. However, how can I read till a specific seperator?
for exmple, if my files are
name profession id # name2 profession3 id2
I would like to read this file as a record. I can do this in perl by defining a record seperator; is there an equivalent in python?
not really; you have to do it manually.
if the file isn't too large, consider reading all of it, and splitting on the
separator:
for record in file.read().spl it(separator):
print record # process record
if you're using a line-oriented separator, like in your example, tools like
itertools.group by can be quite handy:
from itertools import groupby
def is_separator(li ne):
return line[:1] == "#"
for sep, record in groupby(file, is_separator):
if not sep:
print list(record) # process record
or you could just spell things out:
record = []
for line in file:
if line[0] == "#":
if record:
print record # process record
record = []
else:
record.append(l ine)
if record:
print record # process the last record, if any
</F> le*******@yahoo .com wrote: Hi, I know that i can do readline() from a file object. However, how can I read till a specific seperator? for exmple, if my files are
name profession id # name2 profession3 id2
I would like to read this file as a record. I can do this in perl by defining a record seperator; is there an equivalent in python? thanks
I don't think so. But in the pyNMS package
( http://sourceforge/net/projects/pynms) there is a module called
"expect", and a class "Expect". With that you can wrap a file object and
use the Expect.read_unt il() method to do what you want.
--
-- ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~
Keith Dart <kd***@kdart.co m>
public key: ID: F3D288E4
=============== =============== =============== =============== ========= le*******@yahoo .com wrote: Hi, I know that i can do readline() from a file object. However, how can I read till a specific seperator? for exmple, if my files are
name profession id # name2 profession3 id2
I would like to read this file as a record. I can do this in perl by defining a record seperator; is there an equivalent in python? thanks
To actually answer your question, there is no equivalent to $| in python.
You need to hand code your own record parser, or else read in the whole
contents of the file and use the string split method to chop it up into
fields.
What about a generator and xreadlines for those really large files:
py>def recordbreaker(r ecordpath, seperator='#'):
.... rec = open(recordpath ,'r')
.... xrecord = rec.xreadlines( )
.... a =[]
.... for line in xrecord:
.... sep = line.find(seper ator)
.... if sep != -1:
.... a.append(line[:sep])
.... out = ''.join(a)
.... a =[]
.... a.append(line[sep+1:])
.... yield out
.... else:
.... a.append(line)
.... if a:
.... yield ''.join(a)
.... rec.close()
....
py>records = recordbreaker('/tmp/myrecords.txt')
py>for item in records:
.... print item
M.E.Farmer le*******@yahoo .com wrote: Hi, I know that i can do readline() from a file object. However, how can I read till a specific seperator? for exmple, if my files are
name profession id # name2 profession3 id2
I would like to read this file as a record. I can do this in perl by defining a record seperator; is there an equivalent in python? thanks
"M.E.Farmer " wrote: What about a generator and xreadlines for those really large files:
when you loop over a file object, Python uses a generator and a xreadlines-
style buffering system to read data as you go. (if you check the on-line help,
you'll notice that xreadlines itself is only provided for compatibility reasons).
or in other words, the examples I posted a couple of hours ago uses no more
memory than your version.
</F>
Fredrik,
Thanks didn't realize that about reading a file on a for loop. Slick!
By the way the code I posted was an attempt at not building a
monolithic memory eating list like you did to return the values in your
second example.
Kinda thought it would be nice to read them as needed instead of all at
once.
I dont have itertools yet. That module looks like it rocks.
thanks for the pointers,
M.E.Farmer
M.E.Farmer wrote: I dont have itertools yet. That module looks like it rocks. thanks for the pointers, M.E.Farmer
If you have python 2.3 or 2.4, you have itertools.
--Scott David Daniels Sc***********@A cm.Org
Yea I should have mentioned I am running python 2.2.2.
Can it be ported to python 2.2.2?
Till they get python 2.4 all up and running....I'll wait a bit.
Thanks for the info,
M.E.Farmer
Scott David Daniels wrote: M.E.Farmer wrote: I dont have itertools yet. That module looks like it rocks. thanks for the pointers, M.E.Farmer
If you have python 2.3 or 2.4, you have itertools.
--Scott David Danies Sc***********@A cm.Org
Scott David Daniels wrote: M.E.Farmer wrote:
I dont have itertools yet. That module looks like it rocks. thanks for the pointers, M.E.Farmer
If you have python 2.3 or 2.4, you have itertools.
for me it seems that 2.3 does not have itertools.group by.
it has itertools, but not itertools.group by.
activepython-2.4:(win) import itertools dir(itertools)
['__doc__', '__name__', 'chain', 'count', 'cycle', 'dropwhile',
'groupby', 'ifilter', 'ifilterfalse', 'imap', 'islice', 'izip',
'repeat', 'starmap', 'takewhile', 'tee']
python-2.3:(linux) import itertools dir(itertools)
['__doc__', '__file__', '__name__', 'chain', 'count', 'cycle',
'dropwhile', 'ifilter', 'ifilterfalse', 'imap', 'islice', 'izip',
'repeat', 'starmap', 'takewhile']
gabor This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Eric Wichterich |
last post by:
Hello Pythonistas,
does anyone know an equivalent python statement for perl's
"next"-statement?
(Perl's "next" skips a loop cycle and continues with the next loop
cycle - it does not abort the whole loop).
Thank you!
Eric
|
by: Edward WIJAYA |
last post by:
Hi,
I am new to Python, and I like to
learn more about it. Since I am
used to Perl before, I would like
to know what is Python equivalent
of Perl code below:
$filename = $ARGV;
|
by: Dmitry |
last post by:
Hello everyone,
I have a really simple question here:
I have a plain space delimited file that I want to read with WHILE
loop 1 line at the time and process each input record as an array of
elements. Then, on the next iteration of the loop, clean up the array
and populate it with contents of the next input record. Each input
record consists of 7 fields / elements separated by spaces. The reason
I need this is because I have to switch...
|
by: championsleeper |
last post by:
we have a system that recives plain text files from numerous external
sources (database and others). our system recieves the information and
then processes it. each line in the input file is a record. our system
recognizes part of the records by character location
(1-6)=date,(7-9)=age etc, parses the record according to the
configuration it reads and proceses the data.
quite often, the quality of the recieved data is not "good" and we...
|
by: Gregg |
last post by:
Hello all,
I have been banging my head over a problem that I am having reading a
comma seperated file (CSV) that can contain from 1 to 10,000 records.
My code snipit is as follows:
**Start code snipit**
Dim strCustFullName as string
Dim strCustAddr1 as string
| |
by: lgo |
last post by:
I have read several variations of this topic posted on this news group.
However I was not able to find the answer for my problem.
I am trying to build code (see below) to update a large single record
using data from a temporary (editing) table with identical field
structure. I iterate through the fields (some 50 of them) with a For
Each - Next loop to update each field with the SQL statement. The
updating works fine with numeric data type...
|
by: arnuld |
last post by:
I have created a program to print the input words on stdout. Input is
taken dynamically from stdin. In each word, each input character is
allocated dynamically. I have ran this program with a file containing a
*single* word made of 25525500 letters and this program works fine on it.
I will welcome any suggestions for improvement.
/* * A program that will ask the user for input and then will print the words on stdout
* and will also...
|
by: sixtyfootersdude |
last post by:
Good Morning!
I am just starting to learn perl and I am somewhat mistifide about when I should do:
print("@input");
and when I should do:
print(@input)
|
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...
|
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,...
|
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,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |