【问题标题】:f.readline() returns text in apostrophes, how to remove it?f.readline() 返回撇号中的文本,如何删除它?
【发布时间】:2018-02-23 23:16:33
【问题描述】:

我需要从 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

【问题讨论】:

  • 只是字符串表示,打印字符串时使用。
  • @heemayl 文件中的一行会以 \n 结尾。
  • @JoshLee 他可能正在用未显示的代码对其进行修剪。
  • 您的输出不可能由您的代码生成。你能告诉我们真实的输出以及它是如何产生的吗?特别是,您所说的“调用'line'”是什么意思?你的代码有循环吗?
  • 你说得对,我会得到'red\n'。但是这个我可以很容易地用 strip() 删除。

标签: python python-3.x text readline apostrophe


【解决方案1】:

正如我所怀疑的,您的问题出在其他地方。以下代码片段读取文件的整个内容:

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

当你执行接下来的三行时,f.readline() 返回一个空字符串'',被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)

【讨论】:

  • 非常感谢。我没有意识到这个事实。必须返工。
【解决方案2】:

我相信你想使用打印功能。

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

当调用一个变量时,Python 会在它周围加上撇号,让你知道它是一个字符串,但如果你打印变量,它只会输出内容。

更新:这似乎不是撇号的问题,但其他人已经解决了这个问题!

【讨论】:

  • 谢谢。我想用它作为对象的颜色:canvas.create_rectangle(x, y, x+a, y+a, fill = line)
  • 无论您使用哪个库,fill 很可能需要是一个字符串。如果您使用 Tkinter,则需要从他们的文档中指定一个十六进制字符串,但是“颜色 'white', 'black', 'red', 'green', 'blue', 'cyan', 'yellow',并且'洋红色'将始终可用。”我认为你不需要去掉使徒。您应该能够将变量“line”作为填充传递。
【解决方案3】:

好的,所以我做了一些小的修改,它现在可以工作了。感谢 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

【讨论】:

    猜你喜欢
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 2020-11-15
    相关资源
    最近更新 更多