简体   繁体   中英

Opening and reading file in python

I am having trouble with opening and reading a file writing python. I am supposed to read values from a .txt file in order to find the maximum, minimum etc.. When I create and array inside the code, like "values = [1, 5, 77]" for example, it works fine. But when I try to open the .txt file, which is in the same map as my .py file, i get errors. Someone help me please!

from statistics import median

max_value = None
min_value = None
avg_value = None
med_value = None

#values = [10, 0, 50, 99, -200, 1313, 7]

#with open("varden.txt") as values:
#   for line in values:
#       print (line)

#values = open("varden.txt", "r")
#   file = values.readlines().split()

with open('varden.txt') as f:
   values = f.readlines().split()

#       max_value = values[0]
            for number in values:
                if number>max_value:
                    max_value = number

#       min_value = values[0]
            for number in values:
                if number<min_value:
                    min_value = number


avg_value = sum(values)/len(values)
med_value = median(values)

values.close()

print ("Maximum value is: ", max_value)
print ("Minimum value is: ", min_value)
print ("Avarage value is: ", avg_value)
print ("Median value is: ", med_value)

Screenshot of code

Errors:

  File "laboration1del2.py", line 21
  for number in values:
                      ^
  TabError: inconsistent use of tabs and spaces in indentation

File "laboration1del2.py", line 21 for number in values: ^ TabError: inconsistent use of tabs and spaces in indentation

^If this is your error, then it is likely referring to your loops which appear to be indented too much. Python depends on formatting, en lieu of something like braces, to interpret code and define scope.

Playing with your posted code, it looks like your loops had too many indents, and also your values variable appears to have 3 spaces indenting it, which seems odd. I've seen tabs generally represented as 4 spaces or 2 spaces, but never 3.

Try this code below:

with open('varden.txt') as f:
    values = f.readlines().split()

    for number in values:
        if number>max_value:
            max_value = number


    for number in values:
        if number<min_value:
            min_value = number

EDIT: You're also declaring values within your with open() block. As a result, I don't think you're going to be able to access it outside of that like you currently are. When that gives you trouble, you're going to be looking to indent these lines as well. so that they are within the scope of that with open() block you've got going on

avg_value = sum(values)/len(values)
med_value = median(values)

values.close()

One issue which I can see in the line values = f.readlines().split() as readlines() returns a list and you are applying split() on a list which is not possible.

Try the below one once,
values = f.readlines()[0].split('=')[1].strip() // [1, 2, 3, 4, 5, 6]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM