Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44234 > unrolled thread

AttributeError Problem

Started byanimemaiden <animemaiden21@gmail.com>
First post2013-04-23 16:41 -0700
Last post2013-04-24 02:17 +0100
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  AttributeError Problem animemaiden <animemaiden21@gmail.com> - 2013-04-23 16:41 -0700
    Re: AttributeError Problem animemaiden <animemaiden21@gmail.com> - 2013-04-23 16:42 -0700
    Re: AttributeError Problem Skip Montanaro <skip@pobox.com> - 2013-04-23 19:02 -0500
      Re: AttributeError Problem animemaiden <animemaiden21@gmail.com> - 2013-04-23 17:28 -0700
        Re: AttributeError Problem MRAB <python@mrabarnett.plus.com> - 2013-04-24 02:17 +0100

#44234 — AttributeError Problem

Fromanimemaiden <animemaiden21@gmail.com>
Date2013-04-23 16:41 -0700
SubjectAttributeError Problem
Message-ID<4dfb3ca3-a539-48ec-aae8-0be22471cea4@googlegroups.com>
Hi,

I'm trying to display a graph in Tkinter  that reads a graph from a file and displays it on a panel which the first line in the file contains a number that indicates the number of vertices (n). The vertices are labeled as 0,1,…,n-1. Each subsequent line, with the format u x y v1, v2, …describes that the vertex u is located at position (x,y) with the edges (u,1). (u,v2), and so on.


I'm using Python 3.2.3 and I keep getting this error:

numberOfVertices = int(infile.readline().decode()) # Read the first line from the file
AttributeError: 'str' object has no attribute 'readline'


Here is my code:

from tkinter import * # Import tkinter
from tkinter import filedialog



def displayGraph(canvas, vertices, edges):
    radius = 3
    for vertex, x, y in vertices:
        canvas.create_text(x - 2 * radius, y - 2 * radius, text = str(vertex), tags = "graph")
        canvas.create_oval(x - radius, y - radius, x + radius, y + radius, fill = "black", tags = "graph")

    for v1, v2 in edges:
        canvas.create_line(vertices[v1][1], vertices[v1][2], vertices[v2][1], vertices[v2][2], tags = "graph")

def main():

    infile = filedialog.askopenfilename()

    
    numberOfVertices = int(infile.readline().decode()) # Read the first line from the file
    print(numberOfVertices)

    vertices = []
    edges = []
    for i in range(numberOfVertices):
        items = infile.readline().strip().split() # Read the info for one vertex
        vertices.append([int(items[0]), int(items[1]), int(items[2])])
        for j in range(3, len(items)):
            edges.append([int(items[0]), int(items[j])])            
    
    print(vertices)
    print(edges)
    
    infile.close()  # Close the input file

    window = Tk() # Create a window
    window.title("Display a Graph") # Set title
    
    frame1 = Frame(window) # Hold four labels for displaying cards
    frame1.pack()
    canvas = Canvas(frame1, width = 300, height = 200)
    canvas.pack()
    
    displayGraph(canvas, vertices, edges)
    
    window.mainloop() # Create an event loop

main()

[toc] | [next] | [standalone]


#44235

Fromanimemaiden <animemaiden21@gmail.com>
Date2013-04-23 16:42 -0700
Message-ID<52948e97-0c4a-44d5-aa54-192e587d62b3@googlegroups.com>
In reply to#44234
On Tuesday, April 23, 2013 7:41:27 PM UTC-4, animemaiden wrote:
> Hi,
> 
> 
> 
> I'm trying to display a graph in Tkinter  that reads a graph from a file and displays it on a panel which the first line in the file contains a number that indicates the number of vertices (n). The vertices are labeled as 0,1,…,n-1. Each subsequent line, with the format u x y v1, v2, …describes that the vertex u is located at position (x,y) with the edges (u,1). (u,v2), and so on.
> 
> 
> 
> 
> 
> I'm using Python 3.2.3 and I keep getting this error:
> 
> 
> 
> numberOfVertices = int(infile.readline().decode()) # Read the first line from the file
> 
> AttributeError: 'str' object has no attribute 'readline'
> 
> 
> 
> 
> 
> Here is my code:
> 
> 
> 
> from tkinter import * # Import tkinter
> 
> from tkinter import filedialog
> 
> 
> 
> 
> 
> 
> 
> def displayGraph(canvas, vertices, edges):
> 
>     radius = 3
> 
>     for vertex, x, y in vertices:
> 
>         canvas.create_text(x - 2 * radius, y - 2 * radius, text = str(vertex), tags = "graph")
> 
>         canvas.create_oval(x - radius, y - radius, x + radius, y + radius, fill = "black", tags = "graph")
> 
> 
> 
>     for v1, v2 in edges:
> 
>         canvas.create_line(vertices[v1][1], vertices[v1][2], vertices[v2][1], vertices[v2][2], tags = "graph")
> 
> 
> 
> def main():
> 
> 
> 
>     infile = filedialog.askopenfilename()
> 
> 
> 
>     
> 
>     numberOfVertices = int(infile.readline().decode()) # Read the first line from the file
> 
>     print(numberOfVertices)
> 
> 
> 
>     vertices = []
> 
>     edges = []
> 
>     for i in range(numberOfVertices):
> 
>         items = infile.readline().strip().split() # Read the info for one vertex
> 
>         vertices.append([int(items[0]), int(items[1]), int(items[2])])
> 
>         for j in range(3, len(items)):
> 
>             edges.append([int(items[0]), int(items[j])])            
> 
>     
> 
>     print(vertices)
> 
>     print(edges)
> 
>     
> 
>     infile.close()  # Close the input file
> 
> 
> 
>     window = Tk() # Create a window
> 
>     window.title("Display a Graph") # Set title
> 
>     
> 
>     frame1 = Frame(window) # Hold four labels for displaying cards
> 
>     frame1.pack()
> 
>     canvas = Canvas(frame1, width = 300, height = 200)
> 
>     canvas.pack()
> 
>     
> 
>     displayGraph(canvas, vertices, edges)
> 
>     
> 
>     window.mainloop() # Create an event loop
> 
> 
> 
> main()

Also, it reads data from a file.

[toc] | [prev] | [next] | [standalone]


#44237

FromSkip Montanaro <skip@pobox.com>
Date2013-04-23 19:02 -0500
Message-ID<mailman.1008.1366761732.3114.python-list@python.org>
In reply to#44234
> numberOfVertices = int(infile.readline().decode()) # Read the first line from the file
> AttributeError: 'str' object has no attribute 'readline'
...
>     infile = filedialog.askopenfilename()

This is just returning a filename.  You need to open it to get a file
object.  For example:

    infile = filedialog.askopenfilename()
    fd = open(infile)
    ...
    numberOfVertices = int(fd.readline().decode())

Skip

[toc] | [prev] | [next] | [standalone]


#44238

Fromanimemaiden <animemaiden21@gmail.com>
Date2013-04-23 17:28 -0700
Message-ID<18292d0f-1f6f-411a-b348-a5f292e2af51@googlegroups.com>
In reply to#44237
On Tuesday, April 23, 2013 8:02:08 PM UTC-4, Skip Montanaro wrote:
> > numberOfVertices = int(infile.readline().decode()) # Read the first line from the file
> 
> > AttributeError: 'str' object has no attribute 'readline'
> 
> ...
> 
> >     infile = filedialog.askopenfilename()
> 
> 
> 
> This is just returning a filename.  You need to open it to get a file
> 
> object.  For example:
> 
> 
> 
>     infile = filedialog.askopenfilename()
> 
>     fd = open(infile)
> 
>     ...
> 
>     numberOfVertices = int(fd.readline().decode())
> 
> 
> 
> Skip
Thanks, but now I have this error AttributeError: 'str' object has no attribute 'decode'

[toc] | [prev] | [next] | [standalone]


#44240

FromMRAB <python@mrabarnett.plus.com>
Date2013-04-24 02:17 +0100
Message-ID<mailman.1009.1366766452.3114.python-list@python.org>
In reply to#44238
On 24/04/2013 01:28, animemaiden wrote:
> On Tuesday, April 23, 2013 8:02:08 PM UTC-4, Skip Montanaro wrote:
>> > numberOfVertices = int(infile.readline().decode()) # Read the first line from the file
>>
>> > AttributeError: 'str' object has no attribute 'readline'
>>
>> ...
>>
>> >     infile = filedialog.askopenfilename()
>>
>>
>>
>> This is just returning a filename.  You need to open it to get a file
>>
>> object.  For example:
>>
>>
>>
>>     infile = filedialog.askopenfilename()
>>
>>     fd = open(infile)
>>
>>     ...
>>
>>     numberOfVertices = int(fd.readline().decode())
>>
>>
>>
>> Skip
> Thanks, but now I have this error AttributeError: 'str' object has no attribute 'decode'
>
You already have a string (the line) that you've read from the file, so
what are you trying to 'decode' anyway? Just remove that erroneous
method call.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web