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

标签 python python-2.7 csv delimiter

我有一个文件,我从中输出了几列信息,准确地说是 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/

相关文章:

python-2.7 - 在 VS 2013 中调试 Python Azure 辅助角色

Python xlsxwriter TypeError : tuple indices must be integers, 不是 str

python - 为什么 int(len(str(k))) 比循环 (while) 快

python - 正整数位移产生负整数......这怎么会发生?

python - 当参数是 Pandas DataFrame 等包对象时的 Docstrings Python 函数

python - Matplotlib 绘图生成器

python-2.7 - 如何用python在pdf中绘制形状?

python - 即使一切都是 unicode ( python 2.7 ),ascii 解码错误

Python:遍历几个csv文件

php - 我如何将 csv 文件中的 utf-8 设置为 phpmyadmin