Thread: Analyzing files

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    Raleigh NC
    Posts
    89
    Rep Power
    10

    Analyzing files


    ok in python after I open a file in python and ask for input of a certain set of numbers... is there a way i can echo/ print the data back on the screen of what the data is between them such as
    the numbers 145 and 150 using the list below
    [code]
    145 stuff is fixed

    146 stuff was fixed again and more bugs created
    147 man this is a tedious task
    148 release was faulty
    149 release almost stable
    150 stable release
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Maybe use a dictionary:

    Code:
    dic = {'145':'stuff is fixed', '146':'stuff was fixed again and more bugs created', '147':'man this is a tedious task', '148':'release was faulty', '149':'release almost stable', '150':'stable release'}
    Then whatever value you get from the file you can print out the corresponding string from the dictionary:

    Code:
    print dic['145']
    stuff is fixed
    Not sure if thats what you are looking for though.
    I'll learn this stuff someday.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    I'm also not sure if I understand you correctly, but if your file looks like this:
    Code:
    144 b
    145 bla
    146 blabla
    147 blablabla
    148 blablabla
    149 blablablabla
    150 blablablablabla
    151 blablablablablalba
    then this could do what I think you want:
    Code:
    f = file('a.txt', 'r')
    dic = {}
    for line in f:
        k,v = line.split()
        dic[k] = v
    
    n1 = input("Number1: ")
    n2 = input("Number2: ")
    
    for i in range(n1,n2):
        print i, dic[str(i)]
    Regards, mawe
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    Raleigh NC
    Posts
    89
    Rep Power
    10
    hmm im getting this error returned

    Code:
    Please Enter The File Name/Location
    Of A File To Open.
    : TtExport.txt
    (33206, 0L, 2, 1, 0, 0, 282198L, 1119013020, 1118962886, 1119013019)
    Enter Your Previous SVN: 145
    Enter Your Latest SVN: 146
    Traceback (most recent call last):
      File "nongui.py", line 38, in ?
        k,v = line.split()
    ValueError: too many values to unpack
    
    C:\Python24>
    the entire code of the program is below

    Code:
    import os
    print """Please Enter The File Name/Location
    Of A File To Open."""
    
    
    text_file1 = raw_input(": ")
    print os.stat(text_file1)
    text_file2 = open(text_file1, "r")
    whole_thing = text_file2.read()
    
    
    SVN1 = raw_input("Enter Your Previous SVN: ")
    
    
    
    SVN2 = raw_input("Enter Your Latest SVN: ")
    
    
    f = file(text_file1, 'r')
    dic = {}
    for line in f:     
        k,v = line.split()
        dic[k] = v
    
    for i in range(SVN1,SVN2):
        print i, dic[str(i)]
    DATA1 = dic[str(i)]
    
    
    txtFile = open(text_file1, 'r')
    for line in txtFile:
    	print line
    
    
    if SVN2 > SVN1 :
    	print DATA1
    		
    	
    		
    if SVN2 == SVN1:
    	print "Congradulations Your Software Is Up-To-Date."
    	
    	
    else :
    	if SVN2 < SVN1 :
    		print """
    			You Entered A Number That Is Older Than
    			Your First Number. Please Try Again."""
    
    choice1 = raw_input ("Do you wish to export the Data, Y/N?: ")
    
    if choice1 == "Y" or choice1 == "y":
    	
    	print ("Now Preparing Data To Be Exported...")
    
    	file_name1 = raw_input ("Enter Desired Filename & Extension: ")
    	text_file3 = open(file_name1, "w")
    	text_file3.write(SVN1 + '\n')
    	text_file3.write(SVN2)
    	text_file3.write(DATA1)
    	text_file3.close()
    	print ("Data Export Successful")
    	
    elif choice1 == "N" or choice1 == "n":
    	print ("Data Exporting Canceled")
    	
    else :
    	print ("Choice Not Recognized.. Now Ending Process")
    	
    
    
    raw_input ("\n\nPress the enter key to exit.")
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    Try this:
    Code:
    ...
    k,v = line.split(' ', 1)
    ...
    so it splits only on the first whitespace.
    If this doesn't work, it would be helpful if you showed us what your textfile looks like.

    Regards, mawe
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    Raleigh NC
    Posts
    89
    Rep Power
    10
    there in lies the problem with the text file.. it contains sensative data for the company i work for and well..... yea its involves their source code... so meh... one day i will know python fluently lol.. thanks though im trying your suggestion now
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    A dictionary may be overkill here, being that you want to access lines by their number you could also use a list. However you might need to add one to the index if you're line starts at 1 and not 0.

    A simple way to get around this would be to fill the first element in the list, making the indexing work correctly

    Code:
    linesList = [None]
    for line in file('sample.txt'):
        line = line.split(' ', 1)
        linesList.append(line[1])
    
    print linesList[100]
    If you have Python2.4 and you really want to use a dictionary they I would suggest a small alteration to Mawes example, and use a generator expression for simplicities sake .

    Code:
    ...
    linesDict = dict(line.split(' ', 1) for line in file('sample.txt'))
    ...
    Take care,

    Mark.
    programming language development: www.netytan.com – Hula

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    Raleigh NC
    Posts
    89
    Rep Power
    10
    yaaa new error time...
    ok after making your changes i recieve
    Code:
    Please Enter The File Name/Location
    Of A File To Open.
    : proto.txt
    (33206, 0L, 2, 1, 0, 0, 131L, 1119017425, 1119017425, 1118934016)
    Enter Your Previous SVN: 3890
    Enter Your Latest SVN: 4000
    Traceback (most recent call last):
      File "nongui.py", line 40, in ?
        k,v = line.split(' ', 1)
    ValueError: need more than 1 value to unpack
    ok and the text file, i have made a mockup with the same structure but even shorter for testing called proto.txt so when prompted for a file that is what i input... as well as the numbers listed.

    proto.txt
    Code:
    SVN List
    SVN1 = 3890
    test data to see if it becomes exported.
    SVN2 = 4000
    more test data
    
    
    This is some more text blah blah.
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by thanatosys
    yaaa new error time...
    ok after making your changes i recieve
    Code:
    Please Enter The File Name/Location
    Of A File To Open.
    : proto.txt
    (33206, 0L, 2, 1, 0, 0, 131L, 1119017425, 1119017425, 1118934016)
    Enter Your Previous SVN: 3890
    Enter Your Latest SVN: 4000
    Traceback (most recent call last):
      File "nongui.py", line 40, in ?
        k,v = line.split(' ', 1)
    ValueError: need more than 1 value to unpack
    ok and the text file, i have made a mockup with the same structure but even shorter for testing called proto.txt so when prompted for a file that is what i input... as well as the numbers listed.

    proto.txt
    Code:
    SVN List
    SVN1 = 3890
    test data to see if it becomes exported.
    SVN2 = 4000
    more test data
    
    
    This is some more text blah blah.
    The problem is likely to do with blank lines in the file. A simple way to handle this would be to check if the line is empty or not. you can do that with a simple if statement like this one:

    Code:
    for line in file('sample.txt'):
        if line:
            k, v = line.split(' ', 1)
            ...
    That said, a better way to do this would be to use a try-except block to capture the ValueError caused by unpacking.

    Code:
    try:
        k, v = line.split(' ', 1)
    except ValueError:
        #Do something to handle the error.
        pass
    Hope this helps,

    Mark.
    Last edited by netytan; June 17th, 2005 at 10:47 AM.
    programming language development: www.netytan.com – Hula

  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    Raleigh NC
    Posts
    89
    Rep Power
    10
    ok i have reading and printing to the screen sorted out... thanks everyone
    a new problem has arisen.. ok i have
    Code:
    DATA1 = line.split('\t')
    	print DATA1
    and that prints correctly but when i try to write the same data to a file with
    Code:
    text_file3.writelines(list(DATA1))
    it only writes the first line of the data, is there anyway to make this write all of the lines... help please
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by thanatosys
    ok i have reading and printing to the screen sorted out... thanks everyone
    a new problem has arisen.. ok i have
    Code:
    DATA1 = line.split('\t')
    	print DATA1
    and that prints correctly but when i try to write the same data to a file with
    Code:
    text_file3.writelines(list(DATA1))
    it only writes the first line of the data, is there anyway to make this write all of the lines... help please
    I don't see why any of the code you posted above wouldn't work; besides the indentation error which I'm assuming happened when you posted the code. You also don't need to convert DATA1 to a list before writing since (if everything goes well) it should already be a list after the split.

    Could you post more of your code so we can see what's going on .

    Take care,

    Mark.
    programming language development: www.netytan.com – Hula

  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    Raleigh NC
    Posts
    89
    Rep Power
    10
    here it is

    Code:
    import os
    
    print """Please Enter The File Name/Location
    Of A File To Open."""
    
    
    
    text_file1 = raw_input(": ")
    f=open(text_file1, 'r')
    print os.stat(text_file1)
    
    
    SVN1 = raw_input("Enter Your Previous SVN: ")
    
    
    
    SVN2 = raw_input("Enter Your Latest SVN: ")
    
    
    
    f = file(text_file1, 'rb')
    dic = {}
    for line in f:
    	    k, v = line(SVN1, SVN2)
    #except ValueError:
    #Do something to handle the error.
    	#pass
    for line in f.readline(1):
    	print readline(1)
    	DATA1 = line.split('\t')
    	print DATA1
    	
    	    
    	       
    
    
    '''
    for DATA1 in txtFile1 range(SVN1, SVN2):
    	DATA1 = range(SVN1,SVN2):
    	print DATA1
    
    if SVN2 > SVN1 :
    	print DATA1
    '''		
    	
    		
    if SVN2 == SVN1:
    	print "Congradulations Your Software Is Up-To-Date."
    	
    	
    else :
    	if SVN2 < SVN1 :
    		print """
    			You Entered A Number That Is Older Than
    			Your First Number. Please Try Again."""
    
    choice1 = raw_input ("Do you wish to export the Data, Y/N?: ")
    
    if choice1 == "Y" or choice1 == "y":
    	
    	print ("Now Preparing Data To Be Exported...")
    
    	file_name1 = raw_input ("Enter Desired Filename & Extension: ")
    	text_file3 = open(file_name1, "w")
    	text_file3.write(SVN1 + '\n')
    	text_file3.write(SVN2 + '\n')
    	#problem arises at the line directly below
    	text_file3.writelines(list(DATA1))
    	
    	text_file3.close()
    	print ("Data Export Successful")
    	
    elif choice1 == "N" or choice1 == "n":
    	print ("Data Exporting Canceled")
    	
    else :
    	print ("Choice Not Recognized.. Now Ending Process")
    	
    
    
    raw_input ("\n\nPress the enter key to exit.")
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok, from what I can see your main problem is that you're trying to write the data outside loop so DATA1 is actually the last line printed in the loop.

    You need to move the writing into the loop or alternatively you could save the modified lines to a list for user later in the program, when you want to write the lines to the file .

    You also don't appear to be using a large portion of the code, particularly the first loop which judging from the posts above should construct a dictionary of lineNumber: lineValue pairs.

    Note: that you should close a file handle before you opening it again if you assign the insane to a variable.

    Honestly I would probably start over here since there seems to be a number of problems that would be easier to solve this way i.e. bad indentation etc. If you’d like I could write a simple example for you tomorrow evening .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com – Hula

  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    Raleigh NC
    Posts
    89
    Rep Power
    10
    yes i do realize that most of the code is unusable due to the fact I wrote it to begin with I didnt realize all of the stuff my superiors would want in it, and things have just been tacked on since then, if you wouldnt mind writing a sample, that would be awesome and I would be very greatful.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    Raleigh NC
    Posts
    89
    Rep Power
    10
    well just as an update I have rewritten the file but my boss just served me by telling me my foundation in python was weak, well i already knew that but lol first time someone has read it. so now im taking a break from writing the program to reading my python book lol.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo