Python, file read n lines


Results 1 to 10 of 10

Thread: Python, file read n lines

  1. #1
    Join Date
    Sep 2002
    Location
    Harlow, UK
    Posts
    1,788

    Python, file read n lines

    Its been a long day for me learning some Python to write a script that will reformat a file.

    The file is split into blocks, each is 5 lines and has a single line between different blocks. I need a way to read in a block in, and then see if the second word in the block is SYNCHRONISATION, if so disregard that block and move onto the next. However I can't figure out how to read in 6 lines at a time. All I can find is:
    Code:
    readlines(  	[sizehint])
        Read until EOF using readline() and return a list containing the lines thus read. If the optional sizehint
    argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes
    (possibly after rounding up to an internal buffer size) are read. Objects implementing a file-like interface
    may choose to ignore sizehint if it cannot be implemented, or cannot be implemented efficiently.
    However the amount of information on each line is different, so the bytevalues will be too, or have I misread? Like I said, its been a long day.

    The file is laid out like so:
    Code:
    1) Block SYNCHRONISATION_A X to Y 31,31 Mode 17
    2) Bus A Cmd F Stat 0
    4) Time: 1:04:05 xxx:xxx
    4)
    5) 0000
    6)
    7) Block D X to Y 05,09 Num 1 xxx yyy zzz
    8) Bus A Cmd B Stat 28
    9) Time 1:29:67 xxx:xxx
    10)
    11) 0000
    12)
    What I'd like is to read in likes 1-6, disregard them because of the SYNCHRONISATION part, and then move onto lines 7-12. Since the second word isn't SYNCHRONISATION I want to take the lines and do something with them.

    I've googled but can't find anything, probably not searching with the right terms If anyone has any suggestions or useful site it'd be much appreciated.
    If you have to ask why you want to install Linux, then perhaps you shouldn't.
    -- Michael D. Watts (Gone but never forgotten)

    Linux is not Windows | Posting Guidelines

    Code Monkey (YouTube)

  2. #2
    Join Date
    Apr 2006
    Posts
    32
    here's something, not the best , but just enough

    Code:
    >>> a =open("test.txt").readlines()
    >>> a
    ['1) Block SYNCHRONISATION_A X to Y 31,31 Mode 17\n', '2) Bus A Cmd F Stat 0\n', '4) Time: 1:04:05 xxx:xxx\n', '4)\n', '5) 0000\n', '6)\n', '7) Block D X to Y 05,09 Num 1 xxx yyy zzz\n', '8) Bus A Cmd B Stat 28\n', '9) Time 1:29:67 xxx:xxx\n', '10)\n', '11) 0000\n', '12)']
    >>> for i in range(0,len(a),6):
    ... 	if "SYNCHRONISATION" in a[i]: 
    ... 		print "found" , a[i]
    ... 
    ... 
    found 1) Block SYNCHRONISATION_A X to Y 31,31 Mode 17
    
    >>> for i in range(0,len(a),6):
    ... 	if "SYNCHRONISATION" in a[i]: 
    ... 		print a[i:i+5]
    ... 
    ['1) Block SYNCHRONISATION_A X to Y 31,31 Mode 17\n', '2) Bus A Cmd F Stat 0\n', '4) Time: 1:04:05 xxx:xxx\n', '4)\n', '5) 0000\n']
    >>>

  3. #3
    Join Date
    Sep 2002
    Location
    Harlow, UK
    Posts
    1,788
    Hi ghostdog74,

    Thanks for the reply it was really helpful. I changed the code a bit and the below is what I'm using now, probably not the best code in the world, but I can sort it out later not!

    Once again thanks for the reply and help!

    Code:
            inFile = open(fileName).readlines()
    	
    	for i in range(0,len(inFile),6):
    		if "SYNCHRONISATION" in inFile[i]: 
    			i = i + 5
    		else:
    			print inFile[i:i+5]
    If you have to ask why you want to install Linux, then perhaps you shouldn't.
    -- Michael D. Watts (Gone but never forgotten)

    Linux is not Windows | Posting Guidelines

    Code Monkey (YouTube)

  4. #4
    Join Date
    Sep 2002
    Location
    Harlow, UK
    Posts
    1,788
    Just wanted to say that it didn't work excatly how I wanted...however I have got it working now. See below.

    Thanks for your help. If theres a more efficient way of doing this, I'd appreciate some pointers
    Code:
    inFile = open(fileName, 'r')
    	
    	commandList = []; count = 0
    	
    	for lines in inFile:
    		commandList.append(lines.split())
    		count += 1
    		if count == 6:
    			if "SYNCHRONISATION" in commandList[0][1]:
    				commandList = []; count = 0
    			else:
    				print commandList[0][1]
    				commandList = []; count = 0
    If you have to ask why you want to install Linux, then perhaps you shouldn't.
    -- Michael D. Watts (Gone but never forgotten)

    Linux is not Windows | Posting Guidelines

    Code Monkey (YouTube)

  5. #5
    Join Date
    Sep 2002
    Location
    Harlow, UK
    Posts
    1,788
    Just one more question, because one the the sections in a file is two numbers, seperated by a comma, 5,16, for example. How could I go about getting just the two numbers?

    As when python splits it, it treats it as one string.
    If you have to ask why you want to install Linux, then perhaps you shouldn't.
    -- Michael D. Watts (Gone but never forgotten)

    Linux is not Windows | Posting Guidelines

    Code Monkey (YouTube)

  6. #6
    Join Date
    Apr 2006
    Posts
    32
    sorry, can you tell me after finding the lines with "SYNCHORONISATION", what exactly do you want to do next ? when i run your code, it gives me only :

    Block
    Block

    so i need to know what exactly are you going to do next, after finding the lines with "SYNCHRONISATION"

  7. #7
    Join Date
    Sep 2002
    Location
    Harlow, UK
    Posts
    1,788
    Basically if I find SYNCHRONISATION in the first line of the block I want to disregard it and move on. Any others I want to rearrange and write out to a file, but I've got that bit sorted, apart from one little thing I need to rearrange these two number, '5,16' so it reads 16 5, and assign them to seperate variables.

    But like I said, as they are written as 5,16 Python reads them as one string.
    Code:
    inFile = open(fileName, 'r')
    
            commandList = []; count = 0
    
            for lines in inFile:
                    commandList.append(lines.split())
                    count += 1
                    if count == 6:
                            if "SYNCHRONISATION" in commandList[0][1]:
                                    commandList = []; count = 0
                            else:
                                    print commandList[0][5]
                                    commandList = []; count = 0
    So if you run that code against the file I've attached you'll find that the output is:
    Code:
    ['5', '16']
    ['5', '9']
    ['5', '9']
    ['5', '9']
    What I need to do is take the 5 and assign it to a variable, and then assign the 16 to another variable. I've tried something like...
    Code:
    num1 = commandList[0][5].rstrip(',')
    But it doesn't seem to work.

    I'm looking around and trying different things at the moment, so if I figure it out, I'll post back, but if you've got any suggestions they'd be appreciated!

    Thanks again for the help one this.
    Attached Files Attached Files
    If you have to ask why you want to install Linux, then perhaps you shouldn't.
    -- Michael D. Watts (Gone but never forgotten)

    Linux is not Windows | Posting Guidelines

    Code Monkey (YouTube)

  8. #8
    Join Date
    Apr 2006
    Posts
    32
    ok, so you just need to make ['5','16'] become ['16','5']?

    >>> a = ['5', '16']
    >>> a.reverse()
    >>> a
    ['16', '5']
    >>>

  9. #9
    Join Date
    Jun 2002
    Location
    Jamaica Plain, MA
    Posts
    458
    I think this is what your looking for.

    >>> a = '05,09'
    >>> a
    '05,09'
    >>> b = a.rsplit(',')
    >>> b
    ['05', '09']
    >>> b.reverse()
    >>> b
    ['09', '05']
    >>> c = b[0]
    >>> c
    '09'
    >>> d = b[1]
    >>> d
    '05'
    >>>
    Last edited by Modorf; 08-24-2006 at 12:43 PM.

  10. #10
    Join Date
    Sep 2002
    Location
    Harlow, UK
    Posts
    1,788
    Hi Modorf,

    Thanks for the reply. Thats just what I wanted!

    Thanks both of you for the help with this, its much appreciated!
    If you have to ask why you want to install Linux, then perhaps you shouldn't.
    -- Michael D. Watts (Gone but never forgotten)

    Linux is not Windows | Posting Guidelines

    Code Monkey (YouTube)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •