-3

Possible Duplicate:
Python how to read N number of lines at a time
Read a File 8 Lines at a Time Python

I am trying to read a file line by line given 8 lines at a time and then use those values as variables in my code

The problem is the way its being read or the method that is reading it doesn't work as a string variable in my code

using Read seems to work but I don't know how to read the file using the read() Function 8 lines at a time.

any idea how I can do this?

Thank You

Edit More detail

I am using the library spynner. I can use any number of code to read the file n lines at a time, but actually using the lines as values in a function for spinner isn't working. For example

I use this to read the file

with open("test.txt") as fin:
    try:
        while True:
            data =  islice(fin, 0, 8)                
            email = next(data)

Then in Spynner I do this

browser.wk_fill('input[name="email"]', email)

Which doesn't get filled in the form. I am not building a bot, or spam tool just messing around really.

So appreciate any feed back / help

*cheers

Community
  • 1
  • 1
townlong
  • 183
  • 1
  • 6
  • 20
  • Please use the search function of this site before asking a question - this one is asked suprisingly often: http://stackoverflow.com/questions/5832856/how-to-read-file-n-lines-at-a-time-in-python?rq=1 http://stackoverflow.com/questions/6335839/python-how-to-read-n-number-of-lines-at-a-time?rq=1 http://stackoverflow.com/questions/6673129/python-2-7-how-to-read-only-a-few-lines-at-a-time-from-a-file?rq=1 http://stackoverflow.com/questions/10249673/python-read-lines-of-website-source-code-100-lines-at-a-time?rq=1 – l4mpi Dec 17 '12 at 17:53
  • 2
    Can you please explain why the answers to your question [Read a File 8 Lines at a Time Python](http://stackoverflow.com/questions/13909150/read-a-file-8-lines-at-a-time-python) were not suitable? – Abhijit Dec 17 '12 at 17:53
  • Hello they were suitable for reading the file but actually using the contents as variables did not work. I am not sure why, the argument to the function that I am using expects something like this "Michael" using the read() function allows it to work but they are seperated as 8 characters... – townlong Dec 17 '12 at 18:04
  • I have updated my question – townlong Dec 17 '12 at 18:18
  • Editing out a question (and replacing the text with "this should be deleted, thank you") is generally not recommended on the site. If you just leave it it will be closed, and you could flag it to ask a moderator to delete it. – David Robinson Dec 17 '12 at 20:55

1 Answers1

1

read reads a number of bytes from a file. You want readline which reads until the end of the line. Call that eight times to get eight lines:

[f.readline() for _ in range(8)]

Alternatively, you can use one of the itertools recipes to group the file into eight-line blocks, and then iterate over them:

from itertools import izip_longest
def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

for block in grouper(8, f):
    # do stuff
    pass

This works because iterating over f is the same as iterating over its lines.

Katriel
  • 114,760
  • 19
  • 131
  • 163