473,657 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fixed length lists from .split()?

I'm reading a file that has lines like

bcsn; 1000000; 1223
bcsn; 1000001; 1456
bcsn; 1000003
bcsn; 1000010; 4567

The problem is the line with only the one semi-colon.
Is there a fancy way to get Parts=Line.spli t(";") to make Parts always
have three items in it, or do I just have to check the length of Parts
and loop to add the required missing items (this one would just take
Parts+=[""], but there are other types of lines in the file that have
about 10 "fields" that also have this problem)?

Thanks!

Bob

Jan 26 '07 #1
6 4073
Bob Greschke <bo*@passcal.nm t.eduwrote:
Is there a fancy way to get Parts=Line.spli t(";") to make Parts always
have three items in it, or do I just have to check the length of Parts
and loop to add the required missing items (this one would just take
Parts+=[""], but there are other types of lines in the file that have
about 10 "fields" that also have this problem)?
>>def nsplit(s, sep, n):
return (s.split(sep) + [""]*n)[:n]
>>nsplit("bcs n; 1000001; 1456", ";", 3)
['bcsn', ' 1000001', ' 1456']
>>nsplit("bcs n; 1000001", ";", 3)
['bcsn', ' 1000001', '']
>>>
Jan 26 '07 #2
On 2007-01-26 11:13:56 -0700, Duncan Booth <du**********@i nvalid.invalids aid:
Bob Greschke <bo*@passcal.nm t.eduwrote:
>Is there a fancy way to get Parts=Line.spli t(";") to make Parts always
have three items in it, or do I just have to check the length of Parts
and loop to add the required missing items (this one would just take
Parts+=[""], but there are other types of lines in the file that have
about 10 "fields" that also have this problem)?
>>>def nsplit(s, sep, n):
return (s.split(sep) + [""]*n)[:n]
>>>nsplit("bcsn ; 1000001; 1456", ";", 3)
['bcsn', ' 1000001', ' 1456']
>>>nsplit("bcsn ; 1000001", ";", 3)
['bcsn', ' 1000001', '']
That's fancy enough. :) I didn't know you could do [""]*n. I never
thought about it before.

Thanks!

Bob

Jan 26 '07 #3
Duncan Booth:
def nsplit(s, sep, n):
return (s.split(sep) + [""]*n)[:n]
Another version, longer:

from itertools import repeat

def nsplit(text, sep, n):
"""
>>nsplit("bcs n; 1000001; 1456", ";", 3)
['bcsn', ' 1000001', ' 1456']
>>nsplit("bcs n; 1000001", ";", 3)
['bcsn', ' 1000001', '']
>>nsplit("bcsn" , ";", 3)
['bcsn', '', '']
>>nsplit("", ".", 4)
['', '', '', '']
>>nsplit("ab.ac .ad.ae", ".", 2)
['ab', 'ac', 'ad', 'ae']
"""
result = text.split(sep)
nparts = len(result)
result.extend(r epeat("", n-nparts))
return result

if __name__ == "__main__":
import doctest
doctest.testmod ()

Bye,
bearophile

Jan 27 '07 #4
On Jan 26, 11:07 am, Bob Greschke <b...@passcal.n mt.eduwrote:
I'm reading a file that has lines like

bcsn; 1000000; 1223
bcsn; 1000001; 1456
bcsn; 1000003
bcsn; 1000010; 4567

The problem is the line with only the one semi-colon.
Is there a fancy way to get Parts=Line.spli t(";") to make Parts always
have three items in it
In Python 2.5 you can use the .partition() method which always returns
a three item tuple:
>>text = '''\
.... bcsn; 1000000; 1223
.... bcsn; 1000001; 1456
.... bcsn; 1000003
.... bcsn; 1000010; 4567
.... '''
>>for line in text.splitlines ():
.... bcsn, _, rest = line.partition( ';')
.... num1, _, num2 = rest.partition( ';')
.... print (bcsn, num1, num2)
....
(' bcsn', ' 1000000', ' 1223')
(' bcsn', ' 1000001', ' 1456')
(' bcsn', ' 1000003', '')
(' bcsn', ' 1000010', ' 4567')
>>help(str.part ition)
Help on method_descript or:

partition(...)
S.partition(sep ) -(head, sep, tail)

Searches for the separator sep in S, and returns the part before
it,
the separator itself, and the part after it. If the separator is
not
found, returns S and two empty strings.
STeVe

Jan 30 '07 #5
This idiom is what I ended up using (a lot it turns out!):

Parts = Line.split(";")
Parts += (x-len(Parts))*[""]

where x knows how long the line should be. If the line already has
more parts than x (i.e. [""] gets multiplied by a negative number)
nothing seems to happen which is just fine in this program's case.

Bob

Feb 1 '07 #6
On Feb 1, 2:40 pm, Bob Greschke <b...@passcal.n mt.eduwrote:
This idiom is what I ended up using (a lot it turns out!):

Parts = Line.split(";")
Parts += (x-len(Parts))*[""]

where x knows how long the line should be. If the line already has
more parts than x (i.e. [""] gets multiplied by a negative number)
nothing seems to happen which is just fine in this program's case.

Bob
Here's a more generic padding one liner:

from itertools import chain,repeat

def ipad(seq, minlen, fill=None):
return chain(seq, repeat(fill, minlen-len(seq)))
>>list(ipad('on e;two;three;fou r'.split(";"), 7, ''))
['one', 'two', 'three', 'four', '', '', '']
>>tuple(ipad(xr ange(1,5), 7))
(1, 2, 3, 4, None, None, None)
George

Feb 2 '07 #7

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

Similar topics

26
9671
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not properly pad with 0s. strQuantity prints as " 4". Six spaces than the value of intQuantity. This is correct. But all the others end up being string objects of only 6 characters long (with the exception of strTotal). The left most positions of the string object are being padded with one...
5
3112
by: David Garamond | last post by:
The MySQL manual recommends that we create a "fixed-length row" if possible, for speed (especially scanning speed). A fixed-length row is a row which is comprised of only fixed-length fields. A fixed-length field takes a fixed amount of bytes for storage (e.g. INT = 4 bytes, CHAR(M) = M bytes, etc). Is there a similar recommendation in PostgreSQL? I notice that most data types are stored in variable-length mode anyway (is cidr and inet...
24
4819
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross multiple lines. It would appear though that every distinct set of data starts with a 'code' that is always the 25 characters long. The text is variable however. Assuming i've read the contents of the file into the string myfile, how do i split my...
1
9413
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all form data in a fixed length structure that is written direct to disk. I need to load the existing files, into a fixed length structure to initialize the form data within the project. I am running into problems with statements like the...
7
9118
by: antar2 | last post by:
Hello, I can not find out how to read a file into a list of lists. I know how to split a text into a list sentences = line.split(\n) following text for example should be considered as a list of lists (3 columns and 3 rows), so that when I make the print statement list , that the word pear appears
2
3159
by: Edwin.Madari | last post by:
#your thought is right. ======================================================= def sizes2fields(sizes): d = begin = 0 for i in sizes: if begin: end = begin + i else: end = i d.append((begin, end))
2
1533
by: Yimin Rong | last post by:
For example, given a string "A, B, C (P, Q, R), D (X, Y , Z)". Would like to split into tokens thusly: a == "A" a == "B" a == "C (P, Q, R)" a == "D (X, Y , Z)"
0
8402
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
8315
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
8734
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...
0
8608
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 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...
0
7341
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...
1
6172
isladogs
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...
0
4164
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
2733
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
1962
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.