简体   繁体   English

如何将列表格式化为字符串?

[英]How to formatting a list into a string?

I am trying to format a string in python that takes arguments as items from a list of names. 我正在尝试在python中格式化一个字符串,该字符串将参数作为名称列表中的项。 The catch is, I want to print all the list items with double quotes and backslash and one after each other in the same string only. 要注意的是,我只想在同一字符串中打印所有带有双引号和反斜杠且一个接一个的列表项。 The code is: 代码是:

list_names=['Alex', 'John', 'Joseph J']
String_to_pring='Hi my name is (\\"%s\\")'%(list_names)

The output should look like this: 输出应如下所示:

'Hi my name is (\"Alex\",\"John\",\"Joseph J\")'

But instead, I keep getting like this: 但是相反,我一直这样:

'Hi my names is (\"['Alex','John','Joseph J']\")'

I've even tried using .format() and json.dumps() but still the same result. 我什至尝试使用.format()json.dumps()但结果仍然相同。

Is there any way to print the desired output or can I only print each list item at a time? 有什么方法可以打印所需的输出,还是可以一次只打印每个列表项?

Without changing much of your code, you could simply format the repr representation of the list that's converted into a tuple. 无需更改大量代码,您只需格式化已转换为元组的列表的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')

If you want to get your exact output, just do some string replacing: 如果要获得准确的输出,只需执行一些字符串替换:

# 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\")

if you're trying to pass string_to_print to some other place, just try the proper way first, it might actually work for you. 如果您尝试将string_to_print传递到其他地方,请先尝试正确的方法,它实际上可能对您string_to_print


If you were mindful enough, you'll find that the previous "improper way" contains a small bug, try this adding "Alex's house" into list_names , the output would look like this: 如果您足够注意,会发现前面的“不正确的方式”包含一个小错误,请尝试将此操作在list_names添加"Alex's house" ,输出如下所示:

Hi my name is (\"Alex\", \"John\", \"Joseph J\", "Alex\"s house")

To take care of that bug, you'll need to have a better way of replacing, by using re.sub() . 要解决该错误,您需要使用re.sub()来更好地进行替换。

from re import sub

list_names = ['Alex', 'John', 'Joseph J', "Alex's house"]
string_to_print = 'Hi my name is %s' % (sub(r'([\'\"])(.*?)(?!\\\1)(\1)', r'\"\2\"', repr(tuple(list_names))))
print(string_to_print)

But if things like this wouldn't happen during your usage, I would suggest to keep using the "improper way" as it's a lot simpler. 但是,如果在使用过程中不会发生这种情况,我建议您继续使用“不正确的方式”,因为它要简单得多。

There is no function for formatting lists as human-friendly strings You have to format lists yourself: 没有将列表格式化为人类友好的字符串的功能您必须自己格式化列表:

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\")

This is one way using format and join : 这是使用formatjoin一种方法:

list_names = ['Alex', 'John', 'Joseph J']
String_to_pring='Hi my name is (\\"{}\\")'.format('\\",\\"'.join(i for i in list_names))

# Hi my name is (\"Alex\",\"John\",\"Joseph J\")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM