formatted input

Discussion in 'Python' started by Bob, Sep 7, 2010.

  1. Bob

    Bob Guest

    Hi All,
    I have another question about formatted input. Suppose I am reading a
    text file, and that I want it to be something like this

    word11 = num11, word12 = num12, word13 = num13 etc...
    word21 = num21, word22 = num12, word23 = num23 etc...
    etc...

    where wordx1 belongs to a certain dictionary of words, say dic1, while
    wordx2 belongs to dic2, the numbers within some range and so on. I was
    wondering if there is something in the standard library I may use to
    check whether the file I am reading has a correct syntax according to
    my rules/dictionaries instead of implementing my own routine that
    would look like
    (pseudocode)
    for each line
    put words into a list
    check condition for each word

    Thanks
     
    Bob, Sep 7, 2010
    #1
    1. Advertising

  2. Bob <> writes:

    > Hi All,
    > I have another question about formatted input. Suppose I am reading a
    > text file, and that I want it to be something like this
    >
    > word11 = num11, word12 = num12, word13 = num13 etc...
    > word21 = num21, word22 = num12, word23 = num23 etc...
    > etc...
    >
    > where wordx1 belongs to a certain dictionary of words, say dic1, while
    > wordx2 belongs to dic2, the numbers within some range and so on. I was
    > wondering if there is something in the standard library I may use to
    > check whether the file I am reading has a correct syntax according to
    > my rules/dictionaries instead of implementing my own routine that
    > would look like
    > (pseudocode)
    > for each line
    > put words into a list
    > check condition for each word



    No, there is no such thing. Either write something from scratch using
    string methods, or use pyparsing which is certainly up to the task (and
    much more)

    Diez
     
    Diez B. Roggisch, Sep 7, 2010
    #2
    1. Advertising

  3. Bob

    Kenny Meyer Guest

    Bob () wrote:
    > Hi All,
    > I have another question about formatted input. Suppose I am reading a
    > text file, and that I want it to be something like this
    >
    > word11 = num11, word12 = num12, word13 = num13 etc...
    > word21 = num21, word22 = num12, word23 = num23 etc...
    > etc...
    >
    > where wordx1 belongs to a certain dictionary of words, say dic1, while
    > wordx2 belongs to dic2, the numbers within some range and so on. I was
    > wondering if there is something in the standard library I may use to
    > check whether the file I am reading has a correct syntax according to
    > my rules/dictionaries instead of implementing my own routine that
    > would look like


    Python's `re` module

    > (pseudocode)
    > for each line
    > put words into a list
    > check condition for each word


    import re

    match_1 = re.compile("^words1")
    match_2 = re.compile("^words2")

    # Return a match object each
    re.match(match_1, "word11")
    re.match(match_2, "word21")


    I'm sure there are might be other ways to do the same thing.

    --
    - Kenny Meyer <>
    To understand recursion, we must first understand recursion.
    --
     
    Kenny Meyer, Sep 7, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    3
    Views:
    438
    Eric Reitmaier
    Oct 19, 2004
  2. TheDD
    Replies:
    4
    Views:
    517
    John Harrison
    May 13, 2004
  3. TheDD
    Replies:
    7
    Views:
    385
    TheDD
    May 15, 2004
  4. dkk

    formatted input/output question

    dkk, Apr 10, 2006, in forum: C Programming
    Replies:
    2
    Views:
    316
    Herbert Rosenau
    Apr 12, 2006
  5. Jason Heyes
    Replies:
    0
    Views:
    274
    Jason Heyes
    Jun 24, 2005
Loading...

Share This Page