2
def computes(text):
    file=open(text)
    line=file.readlines()
    for i in range(0,len(line)):
        print(str(line[i][1]))

Output

[4, 4, 3, 4, 0]
[9, 7, 6, 8, 0]
[6, 6, 4, 2, 2]
[9, 10, 6, 8, 6]
[12, 9, 6, 9, 2]
[12, 12, 2, 4, 1]
[5, 6, 3, 4, 16]
[24, 21, 14, 16, 0]
[3, 2, 2, 5, 0]
[10, 10, 10, 0, 9]
4
9
6
9
1
1
5
2
3
1

What I'm trying to do, is loop through a text that has an array of lists inside and I am trying to print the number 12 but is only printing the first number, I am not sure where to go from here. How can I correct this?

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
Laura
  • 29
  • 1
  • 2
    `line[i]` is a string, not a list, so `line[i][1]` is the 2nd character of each line. – Barmar Apr 03 '18 at 18:15
  • Why does the output include the whole lines from the file? Your function doesn't print the input anywhere. – Barmar Apr 03 '18 at 18:17
  • I had to stare at this question for 3 minutes with a confused look on my face until I finally realized that the content of the text file is included with the output of the text for whatever reason, and then it took me another minute to figure out why the expected output is 12 instead of 1. – Aran-Fey Apr 03 '18 at 18:24
  • what I'm trying to do, is try to print the first number from every list. In one of the list, the first number is 12 but when i print, the only number that is output is the number 1 and I'm trying to output the whole number 12 – Laura Apr 03 '18 at 18:32
  • @Laura Check my answer below, if that helps, [don't forget to accept and upvote](https://stackoverflow.com/help/someone-answers) :) – Anshul Goyal Apr 03 '18 at 20:59

3 Answers3

1

Your data in the file is stored as text, which means the lists are just string representation and not actual Python objects. So you need to convert them into Python lists for printing the whole number. You can do this by doing a ast.literal_eval on each of the list:

import ast

def computes(text):
    file=open(text)
    line=file.readlines()
    for i in range(0,len(line)):
        print(ast.literal_eval(line[i])[0])
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
1

if you file is in this format, separated number for one space:

  • 4 4 3 4 0
  • 9 7 6 8 0
  • 10 11 12 13

you can use this code:

def computes(text):
    file=open(text)
    for line in file:
        numbers = line.split(" ")
        for number in numbers:
            print(number)
computes("text.txt")
blacker
  • 768
  • 1
  • 10
  • 13
0
    print(str(line[i][1]))

You are printing only the second character of each line - using [1] index, which corresponds to the second element.

If you want to print the whole line, do:

print(str(line[i]))

I'm not sure if that's what you meant, though.

Epion
  • 458
  • 3
  • 7
  • what I'm trying to do, is try to print the first number from every list. In one of the list, the first number is 12 but when i print, the only number that is output is the number 1 and I'm trying to output the whole number 12 – Laura Apr 03 '18 at 18:31