[BangPypers] File read

steve steve at lonetwin.net
Mon Nov 7 18:26:52 CET 2011


Hey Shashidhar,

Others may answer your question, I am not going to do that right away. I'll just 
offer a few suggestions to clean up your code. If you do that, I promise to 
answer your questions:

a. Use os.path.join() to join path components

b. You don't /have/ to read all the lines of the file into a list before 
processing it. The pythonic way would be:

with open(filename) as input_file:
     for line in input_file.readline():
	...do stuff ...

c. You don't need to iterate over lists using numeric indexes. Even if you 
/need/ numeric index, use enumerate. So, instead of :

for index in range(len(somelist)):
     ...do something with somelist[index] ...
         OR
     ...do something with index ...

use something like:

for element in somelist:
    ...do something with element...
     OR
for index, element in enumerate(somelist):
    ...do something with index or element ....


d. Don't chain string operations to just match a substring, use regular 
expressions. If you /have/ to chain string operations (for instance, to extract 
rather than match), chain them only once. So, instead of :

if all_lines_in_file[line].split('->')[1].split(')')[0].split("'")[1].strip() == 
"true":
...
...
      print "ZMP value %s" % 
all_lines_in_file[line].split('->')[1].split(')')[0].split("'")[1].strip()

...and more such monstrosities, do either:

stuff_that_interests_me = line.split('->')[1].split(')')[0]("'")[1].strip()

if stuff_that_interests_me == 'true':
     ....

or just:
matched = re.search("->\((.*)\)", line) # or some such, depending on the format

if matched:
     stuff_that_interests_me = matched.group()
     ....

e. If you need one of three options as values of a dict (which I assume will 
serve as a config dict), default the config keys to something. So, do:

config = {'MyFirstKey' : 'ASK',
           'MyNextKey'  : 'ASK',
           ...
           }

for line in all_lines:
     ...extract stuff_that_interests_me from line...
     if 'MyFirstKey' in line:
         if stuff_that_interests_me == 'true':
             config['MyFirstKey'] = 'ON'
         elif stuff_that_interests_me == 'false':
             config['MyFirstKey'] = 'OFF'
...


HTH,
cheers,
- steve


On 11/07/2011 05:17 PM, Shashidhar Paragonda wrote:
> Dear Python hackers,
>
>    I have text file. I read each line and store it in List.
>
>    my requirement is I need to check only  6 special lines from i.e
>
>    1. resultToPDFFile
>    2. resultToPDFFileDialog
>    3. resultToQdasFile
>    4. resultToQdasFileDialog
>    5. resultToTableFile
>    6. resultToTableFileDialog
>
>   Note: each line have value set in the end eg:  "resultToPDFFile '->' true"
>
>    my conditions are:
>     if first line mentioned above found check the value if it "true"
>               set dictionary item D['PDF'] = "ON"
>     else if value is not "true" then
>             check for line resultToPDFFileDialog if value is "off"
>                set dictionary item D['PDF'] = "OFF"
>             else:
>               set dictionary item D['PDF'] = "ASK"
>
> for example these are the lines in file:
>
>     resultToPDFFile  '->' 'true'
>
>      resultToPDFFileDialog  '->'  'on'
>
>   >>  I have put if else condition statements exactly same as I mentioned
> above.
>   >>  now my problem is though the resultToPDFFile is "true"
>   >>  first time I get the value as ON for dictionary item D['PDF']
>   >>  When loop starts for finding other lines i.e numbers 3&  4 from
> above list my Dictionary   item D['PDF'] is becoming "ASK"
>   >>  the loop is entering into last else part.
>   >>  what is problem I am not gotting
>   >>  is my if else structure is wrong or not I am totally not getting
> other idea please help
>   >>  thank you in advance.
>
> the code is:
>
>>>>
>
>              inspset_file_pointer = open("%s/%s" % (self.inspsetfile_path,
> self.inspection_file),  "r")
>              # Read all lines from the INSPSET file from the TEMPORARY
> directory path
>              all_lines_in_file = inspset_file_pointer.readlines()
>              inspset_file_pointer.close()
>              # loop through all the lines from the file
>              print "readlines from the file"
>              for line in range(0, len(all_lines_in_file)-1):
>                  time.sleep(.5)
>                  if "measPointsForLaterEval" in all_lines_in_file[line]:
>                      print "ZMP line found %s" % all_lines_in_file[line]
>                      if
> all_lines_in_file[line].split('->')[1].split(')')[0].split("'")[1].strip()
> == "true":
>                          time.sleep(.3)
>                          print all_lines_in_file[line]
>                          print "ZMP value %s" %
> all_lines_in_file[line].split('->')[1].split(')')[0].split("'")[1].strip()
>                          self.cnc_parameters['ZMP'] = "ON"
>                          print "dictionary values in ZMP %s" %
> self.cnc_parameters
>                          exit
>                      elif "measPointsForLaterEvalDialog" in
> all_lines_in_file[line]:
>                          if
> all_lines_in_file[line+1].split(')')[0].split('#')[1] == "off":
>                              time.sleep(.3)
>                              print all_lines_in_file[line]
>                              print "ZMP dialog value %s" %
> all_lines_in_file[line+1].split(')')[0].split('#')[1]
>                              self.cnc_parameters['ZMP'] = "OFF"
>                              print "dictionary values in ZMP %s" %
> self.cnc_parameters
>                              exit
>                          else:
>                              self.cnc_parameters['ZMP'] = "ASK"
>
>                  elif "resultsToPDFFile" in all_lines_in_file[line]:
>                      print "PDF line found %s" % all_lines_in_file[line]
>                      # check if current line i.e resultToPDFFile line has
> value is 'true'
>                      # same verification lines are same for rest of the
> keywords
>                      if
> all_lines_in_file[line].split('->')[1].split(')')[0].split("'")[1].strip()
> == "true":
>                          time.sleep(.3)
>                          print all_lines_in_file[line]
>                          print "PDF value %s" %
> all_lines_in_file[line].split('->')[1].split(')')[0].split("'")[1].strip()
>                          self.cnc_parameters['PDF'] = "ON"
>                          print "dictionary values in PDF %s" %
> self.cnc_parameters
>                      elif "resultToExportToPDFDialog" in
> all_lines_in_file[line]:
>                          if
> all_lines_in_file[line+1].split(')')[0].split('#')[1] == "off":
>                              time.sleep(.3)
>                              print all_lines_in_file[line]
>                              print "PDF dialog value %s" %
> all_lines_in_file[line+1].split(')')[0].split('#')[1]
>                              self.cnc_parameters['PDF'] = "OFF"
>                              print "dictionary values in PDF %s" %
> self.cnc_parameters
>                          else:
>                              self.cnc_parameters['PDF'] = "ASK"
>
>                  elif "resultToQdasFile" in all_lines_in_file[line]:
>                      print "Qdas line found %s" % all_lines_in_file[line]
>                      if
> all_lines_in_file[line].split('->')[1].split(')')[0].split("'")[1].strip()
> == "true":
>                          time.sleep(.3)
>                          print all_lines_in_file[line]
>                          print "QDAS value %s" %
> all_lines_in_file[line].split('->')[1].split(')')[0].split("'")[1].strip()
>                          self.cnc_parameters['QDAS'] = "ON"
>                          print "dictionary values in QDAS %s" %
> self.cnc_parameters
>                          exit
>                      elif "resultToQdasFileDialog" in
> all_lines_in_file[line]:
>                          if
> all_lines_in_file[line+1].split(')')[0].split('#')[1] == "off":
>                              time.sleep(.3)
>                              print all_lines_in_file[line]
>                              print "QDAS dialog value %s" %
> all_lines_in_file[line+1].split(')')[0].split('#')[1]
>                              self.cnc_parameters['QDAS'] = "OFF"
>                              print "dictionary values in QDAS %s" %
> self.cnc_parameters
>                              exit
>                          else:
>                              self.cnc_parameters['QDAS'] = "ASK"
>
>                  elif "resultToTableFile" in all_lines_in_file[line]:
>                      print "HDR line found %s" % all_lines_in_file[line]
>                      if
> all_lines_in_file[line].split('->')[1].split(')')[0].split("'")[1].strip()
> == "true":
>                          time.sleep(.3)
>                          print all_lines_in_file[line]
>                          print "HDR value %s" %
> all_lines_in_file[line].split('->')[1].split(')')[0].split("'")[1].strip()
>                          self.cnc_parameters['HDR'] = "ON"
>                          print "dictionary values in HDR %s" %
> self.cnc_parameters
>                          exit
>                      elif "resultToTableFileDialog" in
> all_lines_in_file[line]:
>                          if
> all_lines_in_file[line+1].split(')')[0].split('#')[1] == "off":
>                              time.sleep(.3)
>                              print all_lines_in_file[line]
>                              print "HDR dialog value %s" %
> all_lines_in_file[line+1].split(')')[0].split('#')[1]
>                              self.cnc_parameters['HDR'] = "OFF"
>                              print "dictionary values in HDR %s" %
> self.cnc_parameters
>                              exit
>                          else:
>                              self.cnc_parameters['HDR'] = "ASK"
>


-- 
random spiel: http://lonetwin.net/
what i'm stumbling into: http://lonetwin.stumbleupon.com/


More information about the BangPypers mailing list