1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Python Gurus : an easy question from a noob

Discussion in 'HTML, Graphics & Programming' started by whitecrook, 15 Feb 2013.

  1. whitecrook

    Sgarrista

    Joined: 22 Aug 2005

    Posts: 8,419

    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: 15 Feb 2013
  2. AHarvey

    Capodecina

    Joined: 6 Mar 2008

    Posts: 10,020

    Location: Stoke area

    My Python is rusty (and basic) but your print line is incorrect. You're telling it to print the 1st and 7th character as you've pointed out. What you need to do is to build a loop that:

    searches the string for the first ' and for the second ' and pulls all the data in between that will give you the file location. You would need the position of each ', then saves the text between these positions to a variable for printing.

    Take the length of the string then count backwards until the start of the year and then print the next 4 characters.

    I'm at work so can't check the resources I normally use, but if you go to Udacity.com and check out the "introduction to Computer Science" course there's a section of searching and formatting text and looking for specific characters etc :)
     
  3. AMMUT

    Gangster

    Joined: 14 Dec 2011

    Posts: 436

    My approach would be to split the lines on the comma delimiter between each text field and store them in an array. Then you can address it the way you have done:

    Code:
    print "FILE", arrLine[1], "YEAR", arrLine[7]
    
    Where arrLine would be the array containing each element of the split line string.

    I wouldn't do character counting because what happens if one of the elements contains something you don't expect, or the first field goes from single integer to double or triple figures?

    More info on the str.split() method here: http://docs.python.org/2/library/stdtypes.html

    Check the documentation for your version of Python.
     
  4. whitecrook

    Sgarrista

    Joined: 22 Aug 2005

    Posts: 8,419

    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
     
  5. Ladforce

    Hitman

    Joined: 17 Feb 2006

    Posts: 898

    Location: Fleet

    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.
     
    Last edited: 15 Feb 2013
  6. whitecrook

    Sgarrista

    Joined: 22 Aug 2005

    Posts: 8,419

    Location: Clydebank

    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