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:
JavaScript
x
2
1
[0], [1], [2], [3]
2
what I need is:
JavaScript
1
2
1
[0] | [1] | [2] | [3]
2
Advertisement
Answer
JavaScript
1
7
1
s = "[0], [1], [2], [3]"
2
3
print s.replace(',', ' |')
4
5
# Output:
6
# [0] | [1] | [2] | [3]
7
will work for your test case.
Alternatively, you could get crazy with something like
JavaScript
1
10
10
1
s = "[0], [1], [2], [3]"
2
3
s = s.split(',')
4
s = map(str.strip, s)
5
s = " | ".join(s)
6
7
print s
8
# Output:
9
# [0] | [1] | [2] | [3]
10
Which may be more flexible depending on your needs.