简体   繁体   English

尝试从字符串中删除空格,但出现错误,提示“ tuple”对象没有属性“ replace”

[英]Trying to remove spaces from a string but getting an error saying 'tuple' object has no attribute 'replace'

I have tried to add some inputs together to form a file name, but they seem to have spaces in between, I have tried to get rid of the spaces using replace, and join. 我试图将一些输入加在一起以形成文件名,但是它们之间似乎有空格,我试图使用replace和join消除空格。 but it says join is not defined, and 'tuple' object has no attribute 'replace'. 但是它说未定义联接,并且“元组”对象没有属性“替换”。 please help my code is: 请帮助我的代码是:

from turtle import Turtle, Screen

    def k100():
            tsave = Turtle()
            tsavescreen = tsave.getscreen()
            pictype = input("What type of photo would you like this to be saved as? (png, jpg, pdf, ...) ")
            name = input("What would you like to call your masterpiece? ")
            fnws = name,".",pictype #fnws means file_name_with_spaces
            join(fnws)
            fnns = fnws.replace(" ", "") #fnns means file_name_no_spaces
            tsavescreen.getcanvas().postscript(file=fnns, colormode='color')
            #open("C:\Users\GURNHH\OneDrive - Rugby School\ICT\Python").close()
            #open("pythontest11.pdf").close()
            print("It has been saved as: ", fnns)
            print("It has been saved as: ", fnns)

As using the , creates a tuple, and what you need is a string, you should do the following: 使用时,创建一个元组,并且您需要一个字符串,您应该执行以下操作:

Change: 更改:

fnws = name,".",pictype

To: 至:

fnws = name, + "." + pictype

Now, fnws is a String variable so you can apply the replace on it. 现在, fnws是一个String变量,因此您可以对其应用替换。

Also, consider to use the strip method, with strip you can remove any characters you would like to, simply writing (default is removing whitespcace): 另外,考虑使用strip方法,使用strip可以删除任何想要的字符,只需编写即可(默认为删除whitespcace):

fnws = (name, + "." + pictype).strip()

string.strip(s[, chars]) string.strip(s [,chars])

Return a copy of the string with leading and trailing characters removed. 返回已删除前导和尾随字符的字符串副本。 If chars is omitted or None, whitespace characters are removed. 如果省略了char或None,则将删除空格字符。 If given and not None, chars must be a string; 如果给定且不为None,则char必须为字符串;否则为false。 the characters in the string will be stripped from the both ends of the string this method is called on. 字符串中的字符将从调用此方法的字符串的两端去除。

if you variables have leading and/or trailing whitespace you can call strip function. 如果变量具有前导和/或尾随空格,则可以调用strip函数。 You use it like this 你这样用

fnws = "{}.{}".format(name.strip(),pictype.strip())

For more information on strip function you can go here strip doc 有关剥离功能的更多信息,请转到此处剥离文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'tuple'对象没有属性'replace' - 'tuple' object has no attribute 'replace' python替换错误。 (AttributeError:'tuple'对象没有属性'replace') - Error in python replace. (AttributeError: 'tuple' object has no attribute 'replace') 获取Python错误:AttributeError:'tuple'对象没有属性'Text' - Getting Python error: AttributeError: 'tuple' object has no attribute 'Text' AttributeError:“ list”对象在尝试删除“ / n”时没有属性“ replace” - AttributeError: 'list' object has no attribute 'replace' while trying to remove '/n' AttributeError:'list'对象在尝试删除字符时没有属性'replace' - AttributeError: 'list' object has no attribute 'replace' when trying to remove character Class Inheritance:错误提示 object 没有属性 - Class Inheritance: error saying that object has no attribute 属性错误:“元组”对象没有属性“值” - Attribute Error: 'tuple' object has no attribute 'values' 属性错误,“元组”object 没有属性“_meta” - attribute error, 'tuple' object has no attribute '_meta' 替换字符串时出现“ AttributeError:'float'对象没有属性'replace'”错误 - Getting “AttributeError: 'float' object has no attribute 'replace'” error while replacing string 我收到 discord.py 错误,说 AttributeError: 'str' object has no attribute 'read' - I'm getting a discord.py error saying AttributeError: 'str' object has no attribute 'read'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM