473,395 Members | 1,443 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,395 software developers and data experts.

Parsing/Splitting Line

I have a text file and each line is a list of values. The values are
not delimited, but every four characters is a value. How do I get
python to split this kind of data? Thanks.

Nov 21 '06 #1
9 5516
ac*****@gmail.com wrote:
I have a text file and each line is a list of values. The values are
not delimited, but every four characters is a value. How do I get
python to split this kind of data? Thanks.
1. Look for "slicing" or "slice" or "slices" in the Python tutorial.
2. Write some code.
3. Run it.

Nov 21 '06 #2
At Tuesday 21/11/2006 02:59, ac*****@gmail.com wrote:
>I have a text file and each line is a list of values. The values are
not delimited, but every four characters is a value. How do I get
python to split this kind of data? Thanks.
>>line = "12340001 2 -3"
for j in range(0,len(line),4):
.... print line[j:j+4], int(line[j:j+4])
....
1234 1234
0001 1
2 2
-3 -3
>>>

--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Nov 21 '06 #3
On 2006-11-21, ac*****@gmail.com <ac*****@gmail.comwrote:
I have a text file and each line is a list of values. The
values are not delimited, but every four characters is a value.
How do I get python to split this kind of data? Thanks.
Check out _Text Processing in Python_, Chapter 2, "PROBLEM:
Column statistics for delimited or flat-record files".
URL:http://gnosis.cx/TPiP/

--
Neil Cerutti
Nov 21 '06 #4
El Martes, 21 de Noviembre de 2006 02:59, ac*****@gmail.com escribió:
I have a text file and each line is a list of values. The values are
not delimited, but every four characters is a value. How do I get
python to split this kind of data? Thanks.
You can define a function very easy to make it. For example you can do:

# split-line in 'n' characters
import sys

def splitLine(line, n):
"""split line in 'n' characters"""
x = 0
y = 0
while line >= n:
y = x + n
if line[x:y] == '':
break
yield line[x:y]
x += n

if __name__ == '__main__':
# i get the line-split from the command line
# but you can get it from a file
for x in splitLine(sys.argv[1], int(sys.argv[2])):
print x

--
Kaufmann Manuel
Nov 21 '06 #5

Neil Cerutti wrote:
On 2006-11-21, ac*****@gmail.com <ac*****@gmail.comwrote:
I have a text file and each line is a list of values. The
values are not delimited, but every four characters is a value.
How do I get python to split this kind of data? Thanks.

Check out _Text Processing in Python_, Chapter 2, "PROBLEM:
Column statistics for delimited or flat-record files".
URL:http://gnosis.cx/TPiP/
Hmmmm ... the elementary notion "do line[start:end] in a loop" is well
buried, just behind this:

# Adjust offsets to Python zero-based indexing,
# and also add final position after the line
num_positions = len(self.column_positions)
offsets = [(pos-1) for pos in self.column_positions]
offsets.append(len(line))

Folk who are burdened with real-world flat files (example: several
hundred thousand lines each of 996 bytes wide) might want to consider
moving the set-up of "offsets" out of the once-per line splitter()
method to the __init__() method :-)

Cheers,
John

Nov 21 '06 #6
Manuel Kaufmann wrote:
El Martes, 21 de Noviembre de 2006 02:59, ac*****@gmail.com escribió:
I have a text file and each line is a list of values. The values are
not delimited, but every four characters is a value. How do I get
python to split this kind of data? Thanks.

You can define a function very easy to make it. For example you can do:

# split-line in 'n' characters
import sys

def splitLine(line, n):
"""split line in 'n' characters"""
x = 0
y = 0
while line >= n:
The intent appears to be that "line" refers to a str object, while "n"
refers to an int object. Comparison of such disparate objects is
guaranteed to produce a reproducible (but not necessarily meaningful)
result.

For example:

| >>'' 2
| True

You need to reconsider what you really want to have happen when there
is a trailing short slice. Possibilities are:

(a) silently ignore it -- what I guess your intent was, but the least
attractive IMO
(b) raise an exception -- overkill IMO
(c) just tack it on the end (which is what your code is currently doing
*accidentally*) -- and mention this in the docs and let the caller do
what they want with it.
y = x + n
if line[x:y] == '':
break
yield line[x:y]
x += n

if __name__ == '__main__':
# i get the line-split from the command line
# but you can get it from a file
for x in splitLine(sys.argv[1], int(sys.argv[2])):
print x
HTH,
John

Nov 21 '06 #7
ac*****@gmail.com wrote:
I have a text file and each line is a list of values. The values are
not delimited, but every four characters is a value. How do I get
python to split this kind of data? Thanks.
I'm a nut for regular expressions and obfuscation...

import re
def splitline(line, size=4):
return re.findall(r'.{%d}' % size, line)
>>splitline("helloiamsuperman")
['hell', 'oiam', 'supe', 'rman']
or if you care about remainders...

import re
def splitline(line, size=4):
return re.findall(r'.{%d}|.+$' % size, line)
>>splitline("helloiamsupermansd")
['hell', 'oiam', 'supe', 'rman', 'sd']
noah
Nov 22 '06 #8
Noah Rawlins wrote:

I'm a nut for regular expressions and obfuscation...

import re
def splitline(line, size=4):
return re.findall(r'.{%d}' % size, line)
>>splitline("helloiamsuperman")
['hell', 'oiam', 'supe', 'rman']
there are laws against such use of regular expressions in certain
jurisdictions.

</F>

Nov 22 '06 #9
Fredrik Lundh schrieb:
Noah Rawlins wrote:

>I'm a nut for regular expressions and obfuscation...

import re
def splitline(line, size=4):
return re.findall(r'.{%d}' % size, line)
> >>splitline("helloiamsuperman")
['hell', 'oiam', 'supe', 'rman']

there are laws against such use of regular expressions in certain
jurisdictions.
.... and in particularly bad cases, you will be punished by Perl
not less than 5 years ...

Georg
Nov 22 '06 #10

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

Similar topics

7
by: Kylotan | last post by:
I have a text file where the fields are delimited in various different ways. For example, strings are terminated with a tilde, numbers are terminated with whitespace, and some identifiers are...
9
by: Rob | last post by:
I am trying to write a program with VC++ 6.0 to read a txt file which looks like this: Network Destination Netmask Gateway Interface Metric 0.0.0.0 0.0.0.0 ...
6
by: John Paulsson | last post by:
Is there a C# pattern or perhaps a .NET framework way of parsing a command lines, supporting quoted filepaths etc? (I'm not talking about the current applications command arguments. I've got...
0
by: Paulers | last post by:
hello VB masters I have an issue that I am hoping you can help me with. I am creating an application that accepts user input via a richtextbox. the user can enter multiple entries and I need to...
3
by: Lone Wolf | last post by:
I want to thank everybody who tried to help me, and also to post my solution, even though I don’t think it is a very good one. Many of you correctly guessed that there was an “\r” included with...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
3
by: Damon Getsman | last post by:
Okay so I'm writing a script in python right now as a dirty fix for a problem we're having at work.. Unfortunately this is the first really non-trivial script that I've had to work with in python...
2
by: Jeff | last post by:
I have some custom tags I'm parsing, they look like this: <edit name="some_value" name2="some_value" name3="value with spaces"> I had just been splitting on whitespace, until I though of the...
3
by: anush | last post by:
I have a log file like this: 2008-07-24 17:20:33 W server: shutting down server 2008-07-24 17:20:33 W client: shutting down client I need to parse this using php. I have split them by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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.