Skip to content
Advertisement

Adding delimiters and removing commas

I have a file from which I’ve output several columns of information, 4 to be exact. At this moment they are separated by commas, but in order for my buddy to feed them into another script, he wants the format to be with ‘|’ as delimiter and the commas removed. The commas follow every set of data, so after my script outputs this:

[0], [1], [2], [3]

what I need is:

[0] | [1] | [2] | [3]

Advertisement

Answer

s = "[0], [1], [2], [3]"

print s.replace(',', ' |')

# Output:
# [0] | [1] | [2] | [3]

will work for your test case.

Alternatively, you could get crazy with something like

s = "[0], [1], [2], [3]"

s = s.split(',')
s = map(str.strip, s)
s = " | ".join(s)

print s
# Output:
# [0] | [1] | [2] | [3]

Which may be more flexible depending on your needs.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement