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. 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.
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
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
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
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
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 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
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>
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |