Reading selected data from text files

S

..:: sjf ::..

Hello Pythoners!

There are a lot of files containing data such as:

file1:
0950 1550

file22:
0952 1552

file3:
1000 1020 1050 1130 1150 1200 1245 1600

file4:
1002 1022 1052 1132 1152 1202 1247 1602

file5:
1005 1025 1055 1135 1155 1205 1250 1605

I want to read from them only that data which produce a following sequence
of data:
=> 0950 0952 1000 1002 1005
=> 1555 1557 1600 1602 1605
 
P

Piet van Oostrum

SJF> Hello Pythoners!
SJF> There are a lot of files containing data such as:

SJF> file1:
SJF> 0950 1550

SJF> file22:
SJF> 0952 1552

SJF> file3:
SJF> 1000 1020 1050 1130 1150 1200 1245 1600

SJF> file4:
SJF> 1002 1022 1052 1132 1152 1202 1247 1602

SJF> file5:
SJF> 1005 1025 1055 1135 1155 1205 1250 1605

SJF> I want to read from them only that data which produce a following sequence
SJF> of data:
SJF> => 0950 0952 1000 1002 1005
SJF> => 1555 1557 1600 1602 1605

How do you get at these values, as 1555 and 1557 aren't in your input files?
 
S

..:: sjf ::..

U¿ytkownik "Piet van Oostrum said:
SJF> Hello Pythoners!
SJF> There are a lot of files containing data such as:

SJF> file1:
SJF> 0950 1550

SJF> file22:
SJF> 0952 1552

SJF> file3:
SJF> 1000 1020 1050 1130 1150 1200 1245 1600

SJF> file4:
SJF> 1002 1022 1052 1132 1152 1202 1247 1602

SJF> file5:
SJF> 1005 1025 1055 1135 1155 1205 1250 1605

SJF> I want to read from them only that data which produce a following
sequence SJF> of data:
SJF> => 0950 0952 1000 1002 1005
SJF> => 1555 1557 1600 1602 1605

How do you get at these values, as 1555 and 1557 aren't in your input
files?

ups...
of course it should be:
=> 0950 0952 1000 1002 1005
=> 1600 1602 1605
 
P

Paul McGuire

..:: sjf ::.. said:
Hello Pythoners!

There are a lot of files containing data such as:

file1:
0950 1550

file22:
0952 1552
Try this:

import sets
import os

uniqueElems = sets.Set()

testdata = """
0950 1550
0952 1552
1000 1020 1050 1130 1150 1200 1245 1600
1002 1022 1052 1132 1152 1202 1247 1602
1005 1025 1055 1135 1155 1205 1250 1605
"""
for line in testdata.split("\n"):
uniqueElems.update( line.split() )
# if running Python 2.3.1 or later, replace previous line with
# uniqueElems |= line.split()

print uniqueElems
elemList = list(uniqueElems)
elemList.sort()
print elemList

# to process all files in directory fileDir
#
#for fnam in os.listdir(fileDir):
# for line in file(fnam):
# uniqueElems.update( line.split() )


Good luck,
-- Paul
 
S

..:: sjf ::..

U¿ytkownik "Piet van Oostrum said:
SJF> Hello Pythoners!
SJF> There are a lot of files containing data such as:

SJF> file1:
SJF> 0950 1550

SJF> file22:
SJF> 0952 1552

SJF> file3:
SJF> 1000 1020 1050 1130 1150 1200 1245 1600

SJF> file4:
SJF> 1002 1022 1052 1132 1152 1202 1247 1602

SJF> file5:
SJF> 1005 1025 1055 1135 1155 1205 1250 1605

SJF> I want to read from them only that data which produce a following
sequence SJF> of data:
SJF> => 0950 0952 1000 1002 1005
SJF> => 1555 1557 1600 1602 1605

How do you get at these values, as 1555 and 1557 aren't in your input
files?

ups...
of course it should be:
=> 0950 0952 1000 1002 1005
=> 1550 1552 1600 1602 1605
 
S

..:: sjf ::..

U¿ytkownik "Paul McGuire said:
Try this:

import sets
import os

uniqueElems = sets.Set()

testdata = """
0950 1550
0952 1552
1000 1020 1050 1130 1150 1200 1245 1600
1002 1022 1052 1132 1152 1202 1247 1602
1005 1025 1055 1135 1155 1205 1250 1605
"""
for line in testdata.split("\n"):
uniqueElems.update( line.split() )
# if running Python 2.3.1 or later, replace previous line with
# uniqueElems |= line.split()
^^^^^^^
OK, but what is that?
 
P

Paul McGuire

..:: sjf ::.. said:
U¿ytkownik "Paul McGuire" <[email protected]> napisa³ w wiadomo¶ci
^^^^^^^
OK, but what is that?
In 2.3.1, the Set() class defines a "|=" operator that will add all elements
of a list to the set.

Also, update() is deprecated in 2.3.1 in favor of union_update(),
intersection_update(), and, um, some other update() that I can't recall off
the top of my head, something like "negative_intersection_update" or
something.

-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top