Parsing file format to ensure file meets criteria




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.

.



Relevant Pages

  • Re: Parsing file format to ensure file meets criteria
    ... There is no limit on the number of clause lines. ... Header lines must precede clause lines. ... elements of the list are strings, even integers are converted to strings ... A list of lists requires 2 subscripts, one for the list and the other ...
    (comp.lang.python)
  • RE: Weird Problem with a Subreport
    ... Ok for the sorting i will try to explain it to you. ... query by writing the SQL I didnt include the Place holder field, ... Do you have a text box in the Goup Header? ... I have a header of shutdown in front of the whole list but the two lists ...
    (microsoft.public.access.reports)
  • RE: Weird Problem with a Subreport
    ... Ok for the sorting i will try to explain it to you. ... query from are being sorted by the place holder field. ... Do you have a text box in the Goup Header? ... I have a header of shutdown in front of the whole list but the two lists ...
    (microsoft.public.access.reports)
  • RE: Weird Problem with a Subreport
    ... Do you have a text box in the Goup Header? ... The problem with the sorting is that the field I usually use for sorting is ... Open the report in design view. ... I have a header of shutdown in front of the whole list but the two lists ...
    (microsoft.public.access.reports)
  • RE: Weird Problem with a Subreport
    ... If you don't want to show the header on the first group, ... Open the report in design view. ... I have a header of shutdown in front of the whole list but the two lists ... The Subreport will only show as many rows as will fit in the area I delegated ...
    (microsoft.public.access.reports)