Reading selected data from text files

Paul McGuire ptmcg at users.sourceforge.net
Fri Feb 27 12:14:20 EST 2004


"..:: sjf ::.." <sjf at autograf.pl> wrote in message
news:c1njho$adr$1 at nemesis.news.tpi.pl...
> Hello Pythoners!
>
> There are a lot of files containing data such as:
>
> file1:
> 0950 1550
>
> file22:
> 0952 1552
>
<snip>
> --
> ..:: sjf ::..
> "Linux is like Wigwam. No gates, no windows... Apache inside ;-)"
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





More information about the Python-list mailing list