f.readline() returns 文字中的撇号,如何去除?
f.readline() returns text in apostrophes, how to remove it?
我需要从 txt 文件中读取颜色列表。例如 color.txt 看起来像这样:
red
blue
green
我做的是:
with open('color.txt') as f:
line = f.readline().strip()
当调用 'line' 我的结果是:
'red'
'blue'
'green'
但是,我需要不带“ ”的输出:
red
blue
green
我的txt文件编码有问题吗?还是我做错了什么?
在此先感谢您的帮助。
更新:
由于对我在这里所做的事情没有清楚的了解,请查看完整代码。该程序应在定义的列数中打印彩色矩形。矩形的数量由文件中的颜色数量定义。每个矩形都有一条一条的颜色。
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
我相信你想使用打印功能。
>>> x = 'hello'
>>> x
'hello'
>>> print(x)
hello
>>>
调用变量时,Python 会在其周围加上撇号,让您知道它是一个字符串,但如果您打印变量,它只会输出内容。
更新:这似乎不是撇号的问题,但其他人已经解决了!
正如我所怀疑的,你的问题出在别处。以下代码片段读取文件的整个内容:
for line in f: #line counter
pocetr += 1
当你执行接下来的三行时,f.readline()
returns一个空字符串''
,它被canvas.create_rectangle
解释为黑色:
for b in range(pocetr): #defined how many rectangles shall be drawn
z += 1
col = f.readline().strip() # Here's the problem
解决方案:通过将第一个循环与第二个循环合并来删除第一个循环。
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 指出我的错误。
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
我需要从 txt 文件中读取颜色列表。例如 color.txt 看起来像这样:
red
blue
green
我做的是:
with open('color.txt') as f:
line = f.readline().strip()
当调用 'line' 我的结果是:
'red'
'blue'
'green'
但是,我需要不带“ ”的输出:
red
blue
green
我的txt文件编码有问题吗?还是我做错了什么? 在此先感谢您的帮助。
更新: 由于对我在这里所做的事情没有清楚的了解,请查看完整代码。该程序应在定义的列数中打印彩色矩形。矩形的数量由文件中的颜色数量定义。每个矩形都有一条一条的颜色。
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
我相信你想使用打印功能。
>>> x = 'hello'
>>> x
'hello'
>>> print(x)
hello
>>>
调用变量时,Python 会在其周围加上撇号,让您知道它是一个字符串,但如果您打印变量,它只会输出内容。
更新:这似乎不是撇号的问题,但其他人已经解决了!
正如我所怀疑的,你的问题出在别处。以下代码片段读取文件的整个内容:
for line in f: #line counter
pocetr += 1
当你执行接下来的三行时,f.readline()
returns一个空字符串''
,它被canvas.create_rectangle
解释为黑色:
for b in range(pocetr): #defined how many rectangles shall be drawn
z += 1
col = f.readline().strip() # Here's the problem
解决方案:通过将第一个循环与第二个循环合并来删除第一个循环。
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 指出我的错误。
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