Python parse csv file - replace commas with colons

前端 未结 6 1239
孤独总比滥情好
孤独总比滥情好 2020-12-11 02:50

I suspect this is a common problem, but I counldn\'t seem to locate the answer. I am trying to remove all commas from a csv file and replace them with colons. I would normal

6条回答
  •  有刺的猬
    2020-12-11 03:15

    If you're just replacing commas with colons, you don't need to use a csv parser at all.

    with open("file.csv", 'r') as f:
        with open("temp.csv", 'w') as t:
            for lines in f:
                new_line = line.replace(",",":")
                t.write(new_line)
    

    The only caveat is that you can't have commas elsewhere in the csv file.

提交回复
热议问题