[英]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.
字符串中的字符将从调用此方法的字符串的两端去除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.