简体   繁体   中英

Python - Getting TypeError with _io.BufferedReader

I'm trying to implement the Prim Algorithm, but keep getting this error and I don't know what to do. Both the files are .txt files, the first one (the one assigned to matrizA) contains an adjacency matrix, while the second one is a list of the vertices names, one in each line.

import networkx as nx

def peso(matrizA, u,v):
    return matrizA[u][v]

def adjac(matrizA, u):
    L = []
    for x in range(len(matrizA)):
        L.insert(0,x)
    return L

def extraiMin(Q):
    q = Q[0]
    Q.remove(Q[0])
    return q

def decKey(Q, K):
    for i in range(len(Q)):
        for j in range(len(Q)):
            if K[Q[i]]< K[Q[j]]:
                s = Q[i]
                Q[i] = Q[j]
                Q[j] = s

def prim(vList, matrizA, r):
    u = 0
    v = 0
    P = [None]*len(vList)
    K = [999999999]*len(vList)

    Q=[0]*len(V)
    for u in range(len(Q)):
        Q[u] = vList[u]

    K[r] = 0
    decKey(Q, K)
    while len(Q) > 0:
        u = extraiMin(Q)   
        Adj = adjac(matrizA, u)
        for v in Adj:
            w = peso(matrizA, u)

            if Q.count(v) > 0 and w < K[v]:
                P[v] = u
                K[v] = w
                decKey(Q, K)
    return P

matrizA = open("C:\TrabalhoGrafos\Prim\MatrizAdjacencia_Pesos.txt", 'rb')
V = open("C:\TrabalhoGrafos\Prim\AirportList_Vertices.txt", 'rb')

P = prim(V, matrizA, 0)
print (P)

The error PyCharm is showing me is this:

Traceback (most recent call last):

File "C:/TrabalhoGrafos/Prim/Prim.py", line 74, in

P = prim(V, matrizA, 0)

File "C:/TrabalhoGrafos/Prim/Prim.py", line 40, in prim

P = [None]*len(vList)

TypeError: object of type '_io.BufferedReader' has no len()

I kept the other functions

You just opened the file with V = open("C:\\TrabalhoGrafos\\Prim\\AirportList_Vertices.txt", 'rb') .
While def prim(vList, matrizA, r) expects a list.
It's wrong because you're passing a file object instead as vList to prim() .
File objects do not have a method called len() : https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects

You should read the lines as a list and pass it to prim() .
Right now you just opened the file , you haven't read the content .

You could have something like:

vList_file = "C:\TrabalhoGrafos\Prim\AirportList_Vertices.txt"
with open(vList_file, "rt") as in_file:
    V = in_file.readlines()
V = [x.strip() for x in V]  # strip '\n' from each element
P = prim(V, matrizA, 0) 
...

Careful, as similar applies for matrizA . In that case you're also just passing a file object that you opened so you will hit a similar error when for x in range(len(matrizA)): executes in def adjac(matrizA, u) .

Also remember that you have text files (as it looks like) so you shouldn't open the file in byte mode (ie using the argument 'rb' ).
More in
https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files


Tiny update/finding on 19 November 2020 : Found an issue on GitHub dated more than two years later than this answer where the same error appears and the solution found there matches the general advise in this answer (in bold).

The issue can be read here as it is public:

https://github.com/ping/instagram_private_api/issues/213

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