Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Converting Values

bquad20bquad20 Posts: 17Member
Hi,

I searched the message board and googled, but still cant
find a solution to my problem. I am reading in a file &
appending the lines I want (no commented lines or blank spaces)
to a list as follows:

while(1):
line = theFile.readline()
if not line : break

# look for UN-commented lines && NO blank lines
if not line.startswith("#") and not line.startswith("
"):
jobsFile.append(line.strip())


theFile.close()

This works great and an example(s) output would be
something like this:
0 0 1,15 * 1 $HOME/usr/bin/o9/recrdCrron.sh
OR
2 12 * * 1 /export/data/rc/jm_wkly_poll.sh


My issue lies in converting this information from
the list into something more readable. I can not
figure out a way to replace characters in the list
being there is nothing to "key" off of. There is no
gurantee on x amounts characters/spaces or back slash
or dollar sign or commas being there, so I cant say
look for "this" and convert.

The thing I do know is, each line (element of the list
0, 1, 2, etc.) will have 6 values and the first 5 of
the values in the list will be numbers I need to convert
to a more readable format (2 12 * * 1 - OR - 0 0 1,15 * 1)

Each value means something & I need to figure out a way to
convert that value when I come across it but I dont know
how to go to each value in the list. I have thought of
using multi-dimmensional lists and multiple for loops, but
still cant get it. Is there something built into python
I could use or does some see anything I am missing?


Any help would be greatly appreciated!

Comments

  • infidelinfidel Posts: 2,900Member
    : Hi,
    :
    : I searched the message board and googled, but still cant
    : find a solution to my problem. I am reading in a file &
    : appending the lines I want (no commented lines or blank spaces)
    : to a list as follows:
    :
    : while(1):
    : line = theFile.readline()
    : if not line : break
    :
    : # look for UN-commented lines && NO blank lines
    : if not line.startswith("#") and not line.startswith("
    "):
    : jobsFile.append(line.strip())
    :
    :
    : theFile.close()
    :
    : This works great and an example(s) output would be
    : something like this:
    : 0 0 1,15 * 1 $HOME/usr/bin/o9/recrdCrron.sh
    : OR
    : 2 12 * * 1 /export/data/rc/jm_wkly_poll.sh
    :
    :
    : My issue lies in converting this information from
    : the list into something more readable. I can not
    : figure out a way to replace characters in the list
    : being there is nothing to "key" off of. There is no
    : gurantee on x amounts characters/spaces or back slash
    : or dollar sign or commas being there, so I cant say
    : look for "this" and convert.
    :
    : The thing I do know is, each line (element of the list
    : 0, 1, 2, etc.) will have 6 values and the first 5 of
    : the values in the list will be numbers I need to convert
    : to a more readable format (2 12 * * 1 - OR - 0 0 1,15 * 1)
    :
    : Each value means something & I need to figure out a way to
    : convert that value when I come across it but I dont know
    : how to go to each value in the list. I have thought of
    : using multi-dimmensional lists and multiple for loops, but
    : still cant get it. Is there something built into python
    : I could use or does some see anything I am missing?
    :
    :
    : Any help would be greatly appreciated!

    Easiest thing to do would be to split the line on spaces. The split method takes an optional limit on the max number of splits to perform:

    [code]
    for line in file('k:\data.txt'):
    #skip over useless lines
    jobsFile.append(line.strip().split(' ',5))
    [/code]

    There are more clever ways to manipulate file lines too, if you're interested in trying them out. Here is an example that uses some of the more obscure python features.

    [code]
    from string import strip
    from pprint import pprint

    def usable(line):
    if line.startswith('#'): return False
    if line.strip() == '': return False
    return True

    split5 = lambda s: s.split(' ', 5)

    usable_lines = filter(usable, file('k:\data.txt'))
    stripped_lines = map(strip, usable_lines)
    split_lines = map(split5, stripped_lines)

    pprint(split_lines)
    [/code]

    Don't worry about it if ths stuff doesn't make sense to you. It took me a while to grok it. lambda statements create new functions that return the result of some expression. filter and map apply a function to every item in a list. In the case of filter, only list items that return True when passed to the function are returned as part of a new list. map returns a list of the function results from every list item.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • bquad20bquad20 Posts: 17Member
    I decided to use your suggestion of using the .split(' ',5)
    and that works great for splitting each item of the list into
    'item'.

    [CODE]
    #output is:
    '2', '0', '*', '*', '*', '/export/data/rc/jm_wkly_poll.sh'
    [/CODE]

    That leads me to to my question, how do I got about manipulating
    that 'item' in the list now that its separated? I am really new
    to Python, more of a Java programmer, and I am un-clear on [B]how &
    where to convert each item. Would I use a for loop? Or would I
    convert the value when I read in the line? Or something else? [/B]

    [CODE]
    def parseIt(f):
    try:
    theFile = open(f)

    while(1):
    line = theFile.readline()
    if not line : break

    # look for UN-commented lines && NO blank lines
    if not line.startswith("#") and not line.startswith("
    "):
    jobsFile.append(line.strip().split(' ',5))

    theFile.close()

    except IOError,e:
    print 'error @ parseIt() >', e
    [/CODE]

    Thanks for all your help!

    bg
  • infidelinfidel Posts: 2,900Member
    : I decided to use your suggestion of using the .split(' ',5)
    : and that works great for splitting each item of the list into
    : 'item'.
    :
    : [CODE]
    : #output is:
    : '2', '0', '*', '*', '*', '/export/data/rc/jm_wkly_poll.sh'
    : [/CODE]
    :
    : That leads me to to my question, how do I got about manipulating
    : that 'item' in the list now that its separated? I am really new
    : to Python, more of a Java programmer, and I am un-clear on [B]how &
    : where to convert each item. Would I use a for loop? Or would I
    : convert the value when I read in the line? Or something else? [/B]
    :
    : [CODE]
    : def parseIt(f):
    : try:
    : theFile = open(f)
    :
    : while(1):
    : line = theFile.readline()
    : if not line : break
    :
    : # look for UN-commented lines && NO blank lines
    : if not line.startswith("#") and not line.startswith("
    "):
    : jobsFile.append(line.strip().split(' ',5))
    :
    : theFile.close()
    :
    : except IOError,e:
    : print 'error @ parseIt() >', e
    : [/CODE]
    :
    : Thanks for all your help!

    Now your code has constructed a list of lists. You can refer to any individual piece of data with subscripts like jobsFile[0][0] (like a two-dimensional array in C).

    You can use a loop:
    [code]
    for jf in jobsFile:
    jf[0] #is the first of the five numbers
    jf[5] #is the path
    [/code]

    You can use more advanced constructs like map() and filter() to operate on all items in jobsFile.

    Just remember that at this point every item is a string. You can use int() to convert the numbers, but strings like '*' and '1,12' will throw an exception because they're not valid numbers:

    [code]
    for jf in jobsFile:
    for n in jf[0:5]:
    try:
    print int(n)
    except ValueError, ex:
    print ex
    [/code]

    jf[0:5] is "slice notation" if you're not used to it. You can use it to refer to a range of items within some collection.



    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • bquad20bquad20 Posts: 17Member

    [B]this works perfectly, thank you![/B]


    : : I decided to use your suggestion of using the .split(' ',5)
    : : and that works great for splitting each item of the list into
    : : 'item'.
    : :
    : : [CODE]
    : : #output is:
    : : '2', '0', '*', '*', '*', '/export/data/rc/jm_wkly_poll.sh'
    : : [/CODE]
    : :
    : : That leads me to to my question, how do I got about manipulating
    : : that 'item' in the list now that its separated? I am really new
    : : to Python, more of a Java programmer, and I am un-clear on [B]how &
    : : where to convert each item. Would I use a for loop? Or would I
    : : convert the value when I read in the line? Or something else? [/B]
    : :
    : : [CODE]
    : : def parseIt(f):
    : : try:
    : : theFile = open(f)
    : :
    : : while(1):
    : : line = theFile.readline()
    : : if not line : break
    : :
    : : # look for UN-commented lines && NO blank lines
    : : if not line.startswith("#") and not line.startswith("
    "):
    : : jobsFile.append(line.strip().split(' ',5))
    : :
    : : theFile.close()
    : :
    : : except IOError,e:
    : : print 'error @ parseIt() >', e
    : : [/CODE]
    : :
    : : Thanks for all your help!
    :
    : Now your code has constructed a list of lists. You can refer to any individual piece of data with subscripts like jobsFile[0][0] (like a two-dimensional array in C).
    :
    : You can use a loop:
    : [code]
    : for jf in jobsFile:
    : jf[0] #is the first of the five numbers
    : jf[5] #is the path
    : [/code]
    :
    : You can use more advanced constructs like map() and filter() to operate on all items in jobsFile.
    :
    : Just remember that at this point every item is a string. You can use int() to convert the numbers, but strings like '*' and '1,12' will throw an exception because they're not valid numbers:
    :
    : [code]
    : for jf in jobsFile:
    : for n in jf[0:5]:
    : try:
    : print int(n)
    : except ValueError, ex:
    : print ex
    : [/code]
    :
    : jf[0:5] is "slice notation" if you're not used to it. You can use it to refer to a range of items within some collection.
    :
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    : [code]
    : $ select * from users where clue > 0
    : no rows returned
    : [/code]
    :
    :

Sign In or Register to comment.