简体   繁体   English

如何将 1 个变量中包含的多个字符串组合成 1 个字符串

[英]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.

相关问题 如何打印变量中包含的unicode字符串的值 - how to print the value of an unicode string contained in a variable 如何删除包含在同一字符串列表中的其他字符串中的字符串? - How can I drop strings contained in other string contained in the same string list? 如何使用循环python将多个字符串解析为字符串变量 - how to parse multiple strings into a string variable using a loop python 如何将 2 个字符串组合为 1 个字符串? - how can i combine 2 strings to 1 string? 如何将多个字符串组合成一个 Python 列表? - How to combine multiple strings into one Python list? 如果包含在一组字符串中,则匹配字符串的Pythonic方法 - Pythonic way to match a string if contained in a set of strings 如何将多个dtype“对象”组合成一个变量? - How to combine multiple dtype 'objects' into a single variable? 检测包含多个字符串的列表中的唯一术语 - Detecting Unique Terms In A List Contained of Multiple Strings Python:如果字符串 1 包含在字符串 2 中,如何检查两个具有重复字母的字符串? - Python: How can I check between two strings that have repeating letters if string 1 is contained within string 2? 如何在python中组合字符串和字符串变量 - How do you combine strings and string variables in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM