-1

So I keep receiving this error:

ValueError: Mixing iteration and read methods would lose data

And 1) I don't quite understand why I'm receiving it, and 2) people with similar problems seem to be doing things with their code which are much more complex than a beginner (like myself) can adapt with.

The idea of my code is to read a data_file.txt and convert each line into its own individual array.

so far I have this:

    array = [] #declaring a list with name '**array**' 
    with open('file.txt','r') as input_file:
    for line in input_file:
        line = input_file.readlines()
        array.append(line)
        print('done 1') #for test purposes
        return array

And I keep recieving an error.

"Value error: Mixing iteration and read methods would lose data "message while extracting numbers from a string from a .txt file using python

The above question seemed to be doing something similar, calling in items for an array, however his code was skipping lines and using a range to call in certain parts of it, I don't need that. All I need is to call in all the lines and have them be made into an array.

Python: Mixing files and loops

In this question, once again, something much more than I can understand was being asked. From what I understood, he just wanted a code that would restart after an error and continue, and the answers were about that part. Once again not what I'm looking for.

Community
  • 1
  • 1
J Malone
  • 3
  • 4
  • 2
    Why are you both `for`-looping over the file and calling `readlines` inside the loop? – user2357112 Jul 15 '16 at 17:38
  • Do you want to contain a list of *n* copies of the entire contents of the file, where *n* is the number of lines in the file? Because based on your code, that seems to be what you're trying to do. – pzp Jul 15 '16 at 17:41
  • OP is probably new to it. So what you are doing op translates roughly to: for each `line` I want that each line to be the whole file. Your code doesnt really make sense since you are grabbing each line with your loop and then doing something sily with each of the lines. – limbo Jul 15 '16 at 17:42
  • 2
    Let's backtrack a second here. You say that you want to "convert each line into its own individual array." What should the contents of those arrays be? – pzp Jul 15 '16 at 17:42
  • In each array of the file (or what would be) is 46 lines of binary, which would be ran through a converter so I could have decimal answers. (000001010000000000000000000000000000000000000000000000000000, and so) – J Malone Jul 15 '16 at 17:51
  • line = input_file.readlines() <-- long story short, you don't need this line – 2-bits Jul 15 '16 at 18:06
  • @2-bits Okay, I think I understand this now. It has been removed from the script. I'd just like clarification that what I am expected my code to do will actually do this without this line, and why every other person I ask says that I either need the line for the code to work or its counter productive? – J Malone Jul 15 '16 at 18:08
  • The loop sets the value of the variable line to the content of the line. However, in your loop, you are reading the entire file into that variable. Python basically read through files using a little pointer to keep track of its place in the file. Using readlines sets that pointer to the end of the file which means that when you come back to the top of the loop, there will be nothing left to read in the file. At least, I believe that's what is happening. My python is admittedly a little fuzzy. I think Mad Physicist has the best overall explanation of what is going on. – 2-bits Jul 15 '16 at 18:59
  • Put another way: your for loop is already reading input_file one line at a time. Your code as is is saying: for each line in the file, read the entire file. As I stated above, this causes problems because you have now set input_file's pointer to the very end of the file, and there is no more data. If execution were allowed to continue, the loop would exit after the first line in the file. I guess they have recognized this as a generally bad scenario. – 2-bits Jul 15 '16 at 19:04
  • Last comment: My guess is that their general way of avoiding this is to not allow you to read a file while you are inside an loop which is doing the same. All clear? – 2-bits Jul 15 '16 at 19:10

2 Answers2

2

To make an array of the lines of the file, with one line per array element:

with open('file.txt','r') as input_file:
    array = input_file.readlines()
return array

since readlines will give you the whole file in one shot. Alternatively,

return list(open('file.txt','r'))

will do the same per the docs.

cxw
  • 16,685
  • 2
  • 45
  • 81
  • How do you know that this is what the OP wants? I think the OP's question is too vague at the moment for anyone to give an accurate answer. – pzp Jul 15 '16 at 17:44
  • @JMalone Even when using only these, *instead of* the code block in your question? (Edited to add the `return` statements) – cxw Jul 15 '16 at 17:48
  • @cxw yes, I have commented out my original block and used the one provided instead. – J Malone Jul 15 '16 at 17:54
  • @JMalone Strange. I must be missing something. Would you please [edit your question](http://stackoverflow.com/posts/38402148/edit) to fix up the indentation in your code block? It is not currently correct (should be an indent or `pass` after the`with`), and I may be making an incorrect assumption about what it actually is. Thanks! – cxw Jul 15 '16 at 17:56
2

The error is pretty much self-explanatory (once you know what it is about), so here goes.

You start with the loop for line in input_file:. File objects are iterable in Python. They iterate over the lines in the file. This means that for each iteration of the loop, line will contain the next line in your file.

Next you read a line manually line = input_file.readlines(). This attempts to read a line from the file, but you are already iterating over the lines in the for loop.

Files are usually read sequentially, with no going backwards. What you end up with is a conflict. If you read a line using readline, the iterator in the loop will be forced to return the line after next since it can not go back. However, it is promising to return the next line. The error is telling you that readline knows that there is an active iterator and that calling it would interfere with the loop.

If you take out line = input_file.readlines(), the loop will do what you expect it to.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264