Hi guys, I am fairly new to python. The problem I am having is manipulating the data that I read from a file. Example: if a line of the file is Monday = 0800, is it possible to break up that line so I can read Monday and 0800 separately and do whatever manipulation to that specific text.

The other problem I am having is if I read a line from a file ex: foo and I want to test if the readline() method has read that line ex:

f = open('file.txt')
line = f.readline()
while(True):
   if line == 'foo':
       print 'read line foo'

When i attempt the code above python reads the line foo but it doesn't execute the print statement. There could be something that is very easy that I am missing. Any help is appreciated! Thanks!

basic framework for this kind of thing is normally something like

sep = '='
with open('myfile.txt') as filein:
    for line in filein:
        record = line.strip().split(sep)
        #process records fields

basic framework for this kind of thing is normally something like

sep = '='
with open('myfile.txt') as filein:
    for line in filein:
        record = line.strip().split(sep)
        #process records fields

Thanks for the reply! I tried out your code and it removes the '='. Now my question is how can I test record in an if statement. Ex:

if record == 'Mon' or '0800':
    print 'Monday at 0800'

When I test that if statement it was true for all records in the file. Is record a string or a different data type. When record prints out it prints . I hope you understand my question. Thanks again!

Thanks for the reply! I tried out your code and it removes the '='. Now my question is how can I test record in an if statement. Ex:

if record == 'Mon' or '0800':
    print 'Monday at 0800'

When I test that if statement it was true for all records in the file. Is record a string or a different data type. When record prints out it prints . I hope you understand my question. Thanks again!

I figured out that problem. I wasn't able to edit it again in the allotted 30mins to edit it.

Now I want to test record against a condition in an if statement. Here is my code:

sep = '='
with open(r'myfile.txt') as filein:
    for line in filein:
        record = line.strip().split(sep)
        y = record.pop()
        print y

Since record is a list data type I want to make it str so i can test it in an if statement against another str. When I print out record without popping it: . After I pop it from the list y only prints out '0800'. Why does it not print out 'Mon'? Thanks again!

pop removes by default last value in list. Other values stay in list like stack (append as push operation).

pop removes by default last value in list. Other values stay in list like stack (append as push operation).

This is what I'm trying to do:

with open(r'myfile.txt') as filein:    
    for line in filein:        
        record = line.strip().split(sep)
        t = record.pop()
        if t == '[Backup Schedule]':
            #now retrieve next item in list without looping again
            #something like record.next()
            #then I would do another if statement like
            #if record.next() == 'Monday':
                #backupStart()

Should I use a queue instead?

Can not answer every tiny piece without big picture of the contents of the file. Your test is basically same as

if line.endswith('=[Backup Schedule]\n') or line == '[Backup Schedule]\n':

Can not answer every tiny piece without big picture of the contents of the file. Your test is basically same as

if line.endswith('=[Backup Schedule]\n') or line == '[Backup Schedule]\n':

I have a txt file that has backup parameters: ex:

backup schedule
mon = 0800
tue = n/a
..etc..
backup source dir
path
backup destination dir
path

then my backup program will interpret this appropriately

Thanks for your help.

Why not use standard ConfigParser module?

Didn't know about that module. Thanks a lot.

Can you take a look at my code that I'm working on and give me your input?

#now retrieve next item in list without looping again
#something like record.next()

Use readlines, which reads the file into a list.

f_data = open('file.txt').readlines()
while ctr in range(5):     ## first 5 only
    print "this_rec", f_data[ctr]
    if ctr > 0:
        print "previous_rec",  f_data[ctr-1]
    print "next_rec", f_data[ctr+1]
    print "-"*50

There are many examples of readlines() use on the web so anything further is not necessary here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.