在 python 中创建竖线分隔列表或数组
Creating a pipe delimited list or array in python
我正在尝试从 python 3 中的字符串创建一个竖线分隔列表或一个数组,我在 for 循环中从 JSON 文件中提取,但我该怎么做这个?
假设我有一些代码可以使用 for 循环从 json 文件中获取字符串“string0”、“string1”、“string2”、“string3”...、“stringN”。
我希望输出看起来像
["string0" | "string1" | "string2" | "string3" | ... | "stringN"]
完成后。
更新的措辞:我有 N 个不同的字符串作为输入。我希望这些字符串在一个管道分隔的对象中,并且可以是可变的和可迭代的。
我原以为会是这样的:
with open("some.json") as f:
db = json.load(f)
strings = []
for i in len(db["stringLocation"]):
string = db["stringLocation"][i]["str"]
strings.append(string, sep = "|")
...或类似的东西。
有办法吗?
在此先感谢您,如果已经以其他形式询问过这个问题,我们深表歉意。我觉得也许我现在没有正确的函数或数据对象来搜索这个。
加入他们...
print(" | ".join(f'"{s}"' for s in strings))
我正在尝试从 python 3 中的字符串创建一个竖线分隔列表或一个数组,我在 for 循环中从 JSON 文件中提取,但我该怎么做这个?
假设我有一些代码可以使用 for 循环从 json 文件中获取字符串“string0”、“string1”、“string2”、“string3”...、“stringN”。
我希望输出看起来像
["string0" | "string1" | "string2" | "string3" | ... | "stringN"]
完成后。
更新的措辞:我有 N 个不同的字符串作为输入。我希望这些字符串在一个管道分隔的对象中,并且可以是可变的和可迭代的。
我原以为会是这样的:
with open("some.json") as f:
db = json.load(f)
strings = []
for i in len(db["stringLocation"]):
string = db["stringLocation"][i]["str"]
strings.append(string, sep = "|")
...或类似的东西。
有办法吗?
在此先感谢您,如果已经以其他形式询问过这个问题,我们深表歉意。我觉得也许我现在没有正确的函数或数据对象来搜索这个。
加入他们...
print(" | ".join(f'"{s}"' for s in strings))