[英]How To Combine Multiple Strings Contained in 1 variable into 1 String
Im taking a really complex way to do this and am really confused but here I go!我采取了一种非常复杂的方法来做到这一点,我真的很困惑,但我走了!
Ok so I basically took the result like a normal dict which is result = j_result['result']
j_result is the variable containing all of the information in the following image.好的,所以我基本上把结果当作普通的 dict result = j_result['result']
j_result 是包含下图中所有信息的变量。
and then with that im skipping title and simply extracting everything inside the result with this script然后使用该即时消息跳过标题并使用此脚本简单地提取结果中的所有内容
def extract_values(obj, key):
arr = []
def extract(obj, arr, key):
if isinstance(obj, dict):
for k, v in obj.items():
if isinstance(v, (dict, list)):
extract(v, arr, key)
elif k == key:
arr.append(v)
elif isinstance(obj, list):
for item in obj:
extract(item, arr, key)
return arr
results = extract(obj, arr, key)
return results
def listToString(s):
And with that value im replacing all of the weird characters such as [, ], ', etc. when I finally have a good string that parsed everything im left with the pretty string, but the majority of them contain a character |当我终于有了一个很好的字符串来解析所有我留下的漂亮字符串时,我用这个值替换了所有奇怪的字符,例如 [、]、' 等,但它们中的大多数都包含一个字符 | seperating the japanese and english names, I used this script to remove everything before that leaving english分隔日文和英文名,我用这个脚本删除了之前的所有内容,然后离开英文
o_string = oe_string.replace(",", "\n")
for l in o_string.split('\n'):
thingy1 = l.split('|')[-1] + "\n"
print(repr(thingy1))
But when I do this it print multiple strings represented through thingy1
但是当我这样做时,它会打印通过thingy1
表示的多个字符串
example: output coming from the print function in the above script示例:来自上述脚本中的打印 function 的 output
'string 1'
'string 2'
'string 3'
I want to combine all of the of these strings into 1 string, and seperate new lines with \n.我想将所有这些字符串组合成 1 个字符串,并用 \n 分隔新行。
So in gist instead of having 3 lines I want something that looks like this:因此,在要点上,我想要的不是 3 行,而是如下所示:
string 1\nstring 2\nstring 3\n
and so on...等等...
Sorry if this isnt the right way to phraze this etc, or for this purpose.对不起,如果这不是表达这个等的正确方法,或者为此目的。 But im really at a lost and anything helps.但我真的很迷茫,任何事情都有帮助。
https://media.discordapp.net/attachments/696842088431485069/701708282162708480/unknown.png?width=720&height=42 https://media.discordapp.net/attachments/696842088431485069/701708282162708480/unknown.png?width=720&height=42
So you have a list of strings and want to combine them into a single string with a separator ( \n
) in between them?因此,您有一个字符串列表,并希望将它们组合成一个字符串,并在它们之间使用分隔符 ( \n
)?
That's what .join
is for!这就是.join
的用途!
l = ['string 1', 'string 2', 'string 3']
joined = '\n'.join(l)
# now joined is 'string 1\nstring 2\nstring 3'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.