gpt4 book ai didi

python - 添加分隔符和删除逗号

转载 作者:太空宇宙 更新时间:2023-11-04 09:14:32 24 4
gpt4 key购买 nike

我有一个文件,我从中输出了几列信息,准确地说是 4 列。此时他们是用逗号隔开的,但是为了让我的 friend 把它们输入到另一个脚本中,他希望格式是带'|'作为分隔符并删除逗号。逗号跟在每组数据之后,所以在我的脚本输出之后:

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

我需要的是:

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

最佳答案

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

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

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

将适用于您的测试用例。

或者,你可能会因为类似的东西而发疯

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

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

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

根据您的需要,这可能会更灵活。

关于python - 添加分隔符和删除逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11216991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com