[英]How to remove commas from printed result?
I need to print this list without the commas, but I can't seem to figure out how.我需要打印不带逗号的列表,但我似乎无法弄清楚如何。
data = (
('Sabrina Bryan,1/3/98,ss9387,f9;s1;m1,99km'),
('Fergus Connon Archer,11/21/89,ss5246,f1;s3,102km'),
('Adrian Harvey,3/3/78,ss1654,m5;s2,72km'),
('Patricia Abigail Wolf,9/5/00,ss0936,f3;s4;m8,134km'),
('Georgina Kramer,6/15/95,ss4837,f5;s2;m1,55km'),
('Glenn Julian Ayala,3/19/90,ss3689,s4;f3,152km'),
('Anita Davila,6/27/91,ss9367,f8,203km'),
('Gertrude Nunez,1/12/97,ss3948,s3;m1,34km'),
('Solomon Burton,8/5/88,ss7364,s2;f1,23km'),
('Rafael John Murray,10/19/01,ss9105,s9;f3,78km')
)
info = input("What would you like to do? ")
if info == 'b':
for line in data:
fname0 = data[0].split(' ')
lname0 = fname0[1].split(',')
bday0 = lname0[1].split('/')
def Sabrina(fname0, bday0):
print(fname0[0], *bday0[0:2], sep = ', ')
print(Sabrina(fname0, bday0))
It is printing "Sabrina, 1, 3" when I want it to print "Sabrina 1 3".当我希望它打印“Sabrina 1 3”时,它正在打印“Sabrina, 1, 3”。 I have tried using the
.replace
command, *bday0[0:2].replace(',', ' ')
, but it just gives me an error.我曾尝试使用
.replace
命令*bday0[0:2].replace(',', ' ')
,但它只是给我一个错误。 Any tips?有小费吗?
I figured out a quick solution for your problem:我想出了一个快速解决您问题的方法:
It is based on the use of F-Strings .它基于使用F-Strings 。 With this formatting-solution, you can quickly format your strings by implementing your variables without losing sight of the format of your output, also adding a return to the function.
使用此格式化解决方案,您可以通过实现变量来快速格式化字符串,而不会忘记 output 的格式,还可以添加对 function 的返回。
def Sabrina(fname0, bday0):
return f"{fname0[0]}, {bday0[0]} {bday0[1]}"
Here is more information related to the f-strings formatting explained .这是与解释的 f 字符串格式相关的更多信息。
It would be a great improvement if you could work around the issue that the values are hardcoded.如果您可以解决值被硬编码的问题,那将是一个很大的改进。 But for a quick implementation this should help.
但是为了快速实施,这应该会有所帮助。
Let me know if this answers your question.让我知道这是否回答了您的问题。 Cheers!
干杯!
data = (
('Sabrina Bryan,1/3/98,ss9387,f9;s1;m1,99km'),
('Fergus Connon Archer,11/21/89,ss5246,f1;s3,102km'),
('Adrian Harvey,3/3/78,ss1654,m5;s2,72km'),
('Patricia Abigail Wolf,9/5/00,ss0936,f3;s4;m8,134km'),
('Georgina Kramer,6/15/95,ss4837,f5;s2;m1,55km'),
('Glenn Julian Ayala,3/19/90,ss3689,s4;f3,152km'),
('Anita Davila,6/27/91,ss9367,f8,203km'),
('Gertrude Nunez,1/12/97,ss3948,s3;m1,34km'),
('Solomon Burton,8/5/88,ss7364,s2;f1,23km'),
('Rafael John Murray,10/19/01,ss9105,s9;f3,78km'))
info = input("What would you like to do? ")
if info == 'b':
for line in data:
fname0 = data[0].split(' ')
lname0 = fname0[1].split(',')
bday0 = lname0[1].split('/')
def Sabrina(fname0, bday0):
return f"{fname0[0]} {bday0[0]} {bday0[1]}"
print(Sabrina(fname0, bday0))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.