-1

I'm attempting to make a pipe delimited list or an array in python 3 from strings I'm pulling out of a JSON file within a for loop, but how do I do this?

Say I have some code that grabs strings "string0", "string1", "string2", "string3", ... , "stringN" from a json file using a for loop.

I want the output to look something like ["string0" | "string1" | "string2" | "string3" | ... | "stringN"] once I'm done.

UPDATED PHRASING: I have N distinct strings as input. I want these strings in an object that's pipe separated and can be mutable and iteratable.

I was expecting there to be something like:

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

...or something like that.

Is there a way to do this?

Thanks in advance, and sorry if this has already been asked in some other form. I feel like perhaps I just don't have the right functions or data objects in mind to search this at the moment.

Bucephalus
  • 77
  • 1
  • 9
  • What I intended to mean is say I have a bunch of individually distinct strings that I am gathering in a for loop. They are not a single string, but N distinct strings. I want to separate them by pipe in some kind of object. – Bucephalus Aug 24 '21 at 00:49
  • @ti7 no not exactly. It appears that's asking how to put together different data objects (i.e. strings and numbers) into one string. I'm saying I have N distinct strings as input, and I want to put them into an object where they are pipe separated. – Bucephalus Aug 24 '21 at 00:52

1 Answers1

2

just join them...

 print(" | ".join(f'"{s}"' for s in strings)) 
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Thanks! This actually got me really close. I just assigned this to a variable and dropped `print()` and it gave exactly the output I wanted. – Bucephalus Aug 24 '21 at 01:07