Parsing file format to ensure file meets criteria

Discussion in 'Python' started by seafoid, Dec 18, 2009.

  1. seafoid

    seafoid Guest

    Hi folks,

    I am new to python and am having some trouble parsing a file.

    I wish to parse a file and ensure that the format meets certain
    restrictions.

    The file format is as below (abbreviated):

    c this is a comment
    p wcnf 1468 817439 186181
    286 32 0
    186191 -198 -1098 0
    186191 98 -1098 1123 0

    Lines beginning c are comment lines and must precede all other lines.

    Lines beginning p are header lines with the numbers being 'nvar', 'nclauses'
    and 'hard' respectively.

    All other lines are clause lines. These must contain at least two integers
    followed by zero. There is no limit on the number of clause lines.

    Header lines must precede clause lines.

    In the above example:
    nvar = 1468
    nclauses = 817439
    hard = 186191

    Now for the interesting part...........

    The first number in a clause line = weight.
    All else are literals.
    Therefore, clause = weight + literals

    weight <= hard
    |literal| > 0
    |literal| <= nvar
    number of clause lines = nclauses

    My attempts thus far have been a dismal failure, computing is so viciously
    logical :confused:

    My main problem is that below:

    fname = raw_input('Please enter the name of the file: ')

    z = open(fname, 'r')

    z_list = [i.strip().split() for i in z]

    #here each line is converted to a list, all nested within a list - all
    elements of the list are strings, even integers are converted to strings

    Question - how are nested lists indexed?

    I then attempted to extract the comment, headers and clauses from the nested
    list and assign them to a variable.

    I tried:

    for inner in z_list:
    for lists in inner:
    if lists[0] == 'c':
    comment = lists[:]
    elif lists[0] == 'p':
    header = lists[:]
    else:
    clause = lists[:]
    print comment, header, clause

    This does not work for some reasons which I understand. I have messed up the
    indexing and my assignment of variables is wrong.

    The aim was to extract the headers and comments and then be left with a
    nested list of clauses.

    Then I intended to converted the strings within the clauses nested list back
    to integers and via indexing, check that all conditions are met. This would
    have involved also converting the numerical strings within the header to
    integers but the actual strings are proving a difficult problem to ignore.

    Any suggestions?

    If my mistakes are irritatingly stupid, please feel free to advise that I
    r.t.f.m (read the f**king manual). However, thus far the manual has helped
    me little.

    Thanking you,
    Seafoid.


    --
    View this message in context: http://old.nabble.com/Parsing-file-format-to-ensure-file-meets-criteria-tp26837682p26837682.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
     
    seafoid, Dec 18, 2009
    #1
    1. Advertising

  2. seafoid

    John Bokma Guest

    John Bokma, Dec 18, 2009
    #2
    1. Advertising

  3. seafoid

    seafoid Guest

    seafoid, Dec 18, 2009
    #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. Marek Kubica

    SimpleXMLServer meets inetd

    Marek Kubica, Mar 12, 2005, in forum: Python
    Replies:
    0
    Views:
    332
    Marek Kubica
    Mar 12, 2005
  2. Simon Strandgaard

    embedded document meets end of file

    Simon Strandgaard, Jun 23, 2004, in forum: Ruby
    Replies:
    3
    Views:
    938
    Hal Fulton
    Jun 23, 2004
  3. libsfan01
    Replies:
    8
    Views:
    147
    Dr John Stockton
    Oct 12, 2006
  4. div n
    Replies:
    5
    Views:
    339
    Robert Klemme
    Aug 12, 2013
  5. Cory Mottice
    Replies:
    2
    Views:
    148
    Dave Angel
    Sep 12, 2013
Loading...

Share This Page