Python 如何将列表格式化为字符串?
我试图在python中格式化一个字符串,该字符串将参数作为名称列表中的项目。 问题是,我想打印所有带有双引号和反斜杠的列表项,并且只在同一个字符串中一个接一个。 代码是:Python 如何将列表格式化为字符串?,python,string,formatting,Python,String,Formatting,我试图在python中格式化一个字符串,该字符串将参数作为名称列表中的项目。 问题是,我想打印所有带有双引号和反斜杠的列表项,并且只在同一个字符串中一个接一个。 代码是: list_names=['Alex', 'John', 'Joseph J'] String_to_pring='Hi my name is (\\"%s\\")'%(list_names) 输出应如下所示: 'Hi my name is (\"Alex\",\"John\",\"Joseph J\")' 'Hi my n
list_names=['Alex', 'John', 'Joseph J']
String_to_pring='Hi my name is (\\"%s\\")'%(list_names)
输出应如下所示:
'Hi my name is (\"Alex\",\"John\",\"Joseph J\")'
'Hi my names is (\"['Alex','John','Joseph J']\")'
Hi my name is (\"Alex\", \"John\", \"Joseph J\", "Alex\"s house")
但我却一直这样:
'Hi my name is (\"Alex\",\"John\",\"Joseph J\")'
'Hi my names is (\"['Alex','John','Joseph J']\")'
Hi my name is (\"Alex\", \"John\", \"Joseph J\", "Alex\"s house")
我甚至尝试过使用.format()
和json.dumps()
但结果仍然相同
是否有任何方法可以打印所需的输出或一次只能打印每个列表项?没有将列表格式化为人性化字符串的功能,您必须自己格式化列表:
names = ",".join(r'\"{}\"'.format(name) for name in list_names)
print(names)
#\"Alex\",\"John\",\"Joseph J\"
print('Hi my name is ({})'.format(names))
#Hi my name is (\"Alex\",\"John\",\"Joseph J\")
没有将列表格式化为人性化字符串的功能,您必须自己格式化列表:
names = ",".join(r'\"{}\"'.format(name) for name in list_names)
print(names)
#\"Alex\",\"John\",\"Joseph J\"
print('Hi my name is ({})'.format(names))
#Hi my name is (\"Alex\",\"John\",\"Joseph J\")
这是一种使用和:
这是一种使用和:
在不改变大部分代码的情况下,您可以简单地格式化转换为元组的列表的
repr
表示形式
# proper way - this is what you actually want
list_names = ['Alex', 'John', 'Joseph J']
string_to_print = 'Hi my name is %s' % (repr(tuple(list_names)))
print(string_to_print)
# Hi my name is ('Alex', 'John', 'Joseph J')
如果要获得准确的输出,只需执行一些字符串替换:
# improper way
list_names = ['Alex', 'John', 'Joseph J']
string_to_print = 'Hi my name is %s' % (repr(tuple(list_names)).replace("\'", '\\"'))
print(string_to_print)
# Hi my name is (\"Alex\", \"John\", \"Joseph J\")
如果您试图将string\u to\u print
传递到其他位置,请先尝试正确的方法,它实际上可能适合您
如果您足够注意,您会发现前面的“不正确的方式”包含一个小错误,请尝试在
列表名称中添加“Alex的房子”
,输出如下所示:
'Hi my name is (\"Alex\",\"John\",\"Joseph J\")'
'Hi my names is (\"['Alex','John','Joseph J']\")'
Hi my name is (\"Alex\", \"John\", \"Joseph J\", "Alex\"s house")
为了解决这个bug,您需要使用re.sub()
,找到更好的替换方法
但是,如果在使用过程中不会发生类似的情况,我建议继续使用“不正确的方式”,因为这会简单得多。在不更改大部分代码的情况下,您可以简单地格式化转换为元组的列表的repr
表示形式
# proper way - this is what you actually want
list_names = ['Alex', 'John', 'Joseph J']
string_to_print = 'Hi my name is %s' % (repr(tuple(list_names)))
print(string_to_print)
# Hi my name is ('Alex', 'John', 'Joseph J')
如果要获得准确的输出,只需执行一些字符串替换:
# improper way
list_names = ['Alex', 'John', 'Joseph J']
string_to_print = 'Hi my name is %s' % (repr(tuple(list_names)).replace("\'", '\\"'))
print(string_to_print)
# Hi my name is (\"Alex\", \"John\", \"Joseph J\")
如果您试图将string\u to\u print
传递到其他位置,请先尝试正确的方法,它实际上可能适合您
如果您足够注意,您会发现前面的“不正确的方式”包含一个小错误,请尝试在列表名称中添加“Alex的房子”
,输出如下所示:
'Hi my name is (\"Alex\",\"John\",\"Joseph J\")'
'Hi my names is (\"['Alex','John','Joseph J']\")'
Hi my name is (\"Alex\", \"John\", \"Joseph J\", "Alex\"s house")
为了解决这个bug,您需要使用re.sub()
,找到更好的替换方法
但是,如果在使用过程中不会发生类似的情况,我建议继续使用“不正确的方法”,因为这会简单得多。对反对者说:我建议的解决方案有什么问题吗?您的方法也很有效,谢谢。也可以通过向%s传递参数来遵循相同的方法吗?%s
是一种旧式的格式设置机制,应该避免向下投票者使用:我建议的解决方案有什么问题吗?您的方法也可以,谢谢。也可以通过将参数传递给%s来遵循相同的方法吗?%s
是一种旧式的格式化机制,应该避免使用。我尝试了使用不正确的方法,但它确实有效。为什么我们将元组作为列表名而不是实际列表传递?列表的格式为方括号,但预期的输出使用括号,而括号表示元组。您可以删除tuple()
,但输出将更改为“Hi my name is[\“Alex\”,…]”
,在tuple\u name之后添加了替换函数,我得到的输出是:Hi my name is(\\\“Alex\”,\\“John\”,\\“Joseph J\”)。反斜杠将被双反斜杠取代,而不是单反斜杠。您没有使用print(),您看到的只是元组名称的repr,它是为了向您显示双引号已转义。实际字符串只包含一个反斜杠。如果你按原样运行我的代码,或者把它写到一个文件中,你会看到只有一个反斜杠。没有考虑“亚历克斯的房子”的情形,因为我只想打印名字。但是谢谢你的升级。我试过用不合适的方法,它真的很有效。为什么我们将元组作为列表名而不是实际列表传递?列表的格式为方括号,但预期的输出使用括号,而括号表示元组。您可以删除tuple()
,但输出将更改为“Hi my name is[\“Alex\”,…]”
,在tuple\u name之后添加了替换函数,我得到的输出是:Hi my name is(\\\“Alex\”,\\“John\”,\\“Joseph J\”)。反斜杠将被双反斜杠取代,而不是单反斜杠。您没有使用print(),您看到的只是元组名称的repr,它是为了向您显示双引号已转义。实际字符串只包含一个反斜杠。如果你按原样运行我的代码,或者把它写到一个文件中,你会看到只有一个反斜杠。没有考虑“亚历克斯的房子”的情形,因为我只想打印名字。但是谢谢你的升级。