-3

I need to read a list of colors from txt file. For example color.txt which looks like this:

red
blue
green

what I do is:

with open('color.txt') as f:
    line = f.readline().strip()

when calling 'line' my result comes as:

'red'
'blue'
'green'

However, I need my output without ' ' as:

red
blue
green

Is the problem in encoding of my txt file? Or am I doing something wrong? Thanks in advance for help.

UPDATE: Since there is no clear understanding on what I do here, please see the full code. The program shall print colorful rectangles in defined number of columns. The amount of rectangles is defined by amount of colors in the file. Each rectangle has color as per line, one by one.

import tkinter
canvas = tkinter.Canvas()
canvas.pack()

def stvor(file, width):
    a = 30
    pocetr = 0
    z = 0
    with open(file, 'r') as f:
        x = 10
        y = 10
        for line in f:   #line counter
            pocetr += 1
        for b in range(pocetr):   #defined how many rectangles shall be drawn
            z += 1
            col = f.readline().strip()  #reading color from a file
            canvas.create_rectangle(x, y, x+a, y+a, fill = col)
            x = x + a
            if z == width:   #if the width reaches certain level continue in new line
                y = y + a
                x = 10
                z = 0
Blacho
  • 87
  • 1
  • 8
  • It's just the string representation, used when printing strings. – heemayl Feb 23 '18 at 23:18
  • @heemayl A line from a file would end with `\n` though. – Josh Lee Feb 23 '18 at 23:18
  • @JoshLee He's probably trimming it in code he hasn't shown. – Barmar Feb 23 '18 at 23:19
  • 4
    Your output could not have been produced by your code. Would you show us the _real_ output and how exactly it was produced? In particular, what do you mean by "calling 'line'"? Does your code have a loop? – DYZ Feb 23 '18 at 23:20
  • You are right, I will get 'red\n'. But this I can easily remove with strip(). – Blacho Feb 23 '18 at 23:25
  • @DyZ I need to use it as a list of colors for rectangles. While I use readline() the rectangle stays blank. 'canvas.create_rectangle(x, y, x+a, y+a, fill = line)' – Blacho Feb 23 '18 at 23:37

3 Answers3

0

I believe you want to use the print function.

>>> x = 'hello'
>>> x
'hello'
>>> print(x)
hello
>>> 

When calling a variable, Python will put apostrophe's around it to let you know it's a string, but if you print the variable it will just output the contents.

UPDATE: It seems that this isn't an issue with apostrophe's but someone else covered that!

  • Thanks. I want to use it as color of an object: canvas.create_rectangle(x, y, x+a, y+a, fill = line) – Blacho Feb 23 '18 at 23:27
  • Whichever library you're using, fill most likely will need to be a string. If you're using Tkinter, from their documentation you need to specify a hex string, but "the colors 'white', 'black', 'red', 'green', 'blue', 'cyan', 'yellow', and 'magenta' will always be available." I don't think you need to strip off the apostophes. You should be able to pass the variable "line" in as fill. – David Wilkinson Feb 23 '18 at 23:55
0

Just as I suspected, your problem is elsewhere. The following code fragment reads the whole contents of the file:

for line in f:   #line counter
    pocetr += 1

When you execute the next three lines, f.readline() returns an empty string '', which is interpreted by canvas.create_rectangle as black:

for b in range(pocetr):   #defined how many rectangles shall be drawn
    z += 1
    col = f.readline().strip() # Here's the problem

Solution: Remove the first loop by combining it with the second loop.

for line in f:   #defined how many rectangles shall be drawn
    z += 1
    col = line.strip()
    canvas.create_rectangle(x, y, x+a, y+a, fill = col)
DYZ
  • 45,526
  • 9
  • 47
  • 76
0

OK, so I did small modification and it works now. Thanks to DyZ for pointing out my mistake.

import tkinter
canvas = tkinter.Canvas()
canvas.pack()

def stvor(file, width):
    a = 30
    pocetr = 0
    z = 0
    for line in open(file):  #simple line counter
        pocetr += 1
    with open(file, 'r') as f:
        x = 10
        y = 10
        for b in range(pocetr):   #how many rectangles shall be drawn
            z += 1
            farba = f.readline().strip().  #read color from line
            canvas.create_rectangle(x, y, x+a, y+a, fill = farba)
            x = x + a
            if z == width:   #if the width reaches certain level continue in new line
                y = y + a
                x = 10
                z = 0
Blacho
  • 87
  • 1
  • 8