Python Gurus : an easy question from a noob

Soldato
Joined
22 Aug 2005
Posts
8,840
Location
Clydebank
I have a text file looking like this:

Code:
(0, 'M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003984.jpg', '003984.jpg', 1879, 2580, 1.0, 'FF', '2001')
(1, 'M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003985.jpg', '003985.jpg', 3569, 2584, 2.0, 'BI', '2001')
(2, 'M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003986.jpg', '003986.jpg', 3573, 2584, 2.0, 'BI', '2001')
(3, 'M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003987.jpg', '003987.jpg', 3573, 2584, 2.0, 'BI', '2001')
(4, 'M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003988.jpg', '003988.jpg', 3569, 2589, 2.0, 'BI', '2001')
(5, 'M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003989.jpg', '003989.jpg', 3578, 2575, 2.0, 'BI', '2001')
(6, 'M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003990.jpg', '003990.jpg', 3546, 2566, 2.0, 'BI', '2001')


I want to read this back in and take the path value, and the year value and do something with them (copy image into a dir named YEAR)

When I try to access the data I'm getting a character reference rather than the item e.g.

Code:
print "\nReading the entire file into a list."
text_file = open("M:\\IMAGES\\FILES\\0_FILEDATA\\1_BOX007_20067.log", "r")
lines = text_file.readlines()
#print lines
print len(lines)
for line in lines:
    print "FILE", line[1], "YEAR", line[7]
text_file.close()

gives out put of
Code:
FILE 0 YEAR \
FILE 1 YEAR \
FILE 2 YEAR \
FILE 3 YEAR \
FILE 4 YEAR \
FILE 5 YEAR \
FILE 6 YEAR \

I.e. the 1 and 7 character

but I want
Code:
FILE M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003984.jpg YEAR 2001
FILE M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003985.jpg YEAR 2001
FILE M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003986.jpg YEAR 2001
FILE M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003987.jpg YEAR 2001
FILE M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003988.jpg YEAR 2001
FILE M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003989.jpg YEAR 2001
FILE M:\\IMAGES\\BSCANS\\1\\BOX007\\20066\\003990.jpg YEAR 2001


what am i doing wrong?
 
Last edited:
Soldato
OP
Joined
22 Aug 2005
Posts
8,840
Location
Clydebank
my text file is a print of a python list in the first place, doesn't python have a python way of importing the line and rebuilding the list without doing it the 'manual' way
 
Soldato
OP
Joined
22 Aug 2005
Posts
8,840
Location
Clydebank
Change print line to:-

Code:
print "FILE %s YEAR %s" % tuple([eval(line)[i] for i in (1,7)])

In answer to your question, eval is the function which will take the string and convert it to a python expression.

sweet looks awesome , will read up on those functions.

meanwhile--- :/ that one line should cut out half of this... this is only my second python prog lol. as using the splitting method meant i had to trim spaces, single quotes, a right hand parentheses
Code:
for line in lines:
    for element in line.rstrip().split(','):
        ls.append( element )
    #   print "FILE", element[1], "YEAR", element[7]
text_file.close()

for l in ls:
    year = ls[7].replace('\'', '')
    year2 = re.sub(r'[^\w]', ' ', year)
    output_dir = root_output_dir+'\\'+year2.strip()
    #print output_dir
    ensure_dir(output_dir)

print ls[1], ls[7]
print root_output_dir
 
Back
Top Bottom