How to write a csv with a comma as the decimal separator?

前端 未结 1 1472
有刺的猬
有刺的猬 2021-01-04 23:07

I am trying to create a european-formatted csv in python. I already set the separator to a semicolon

writer = csv.writer(response, delimiter=\';\', quoting=c         


        
相关标签:
1条回答
  • 2021-01-04 23:20

    A little bit hacky way, but it's the best I can think of: convert floats to strings and replace . with ,:

    def localize_floats(row):
        return [
            str(el).replace('.', ',') if isinstance(el, float) else el 
            for el in row
        ]
    
    for row in rows:
        writer.writerow(localize_floats(row))
    

    If you want better localization handling, I suggest you convert all numbers using babel.numbers package.

    0 讨论(0)
提交回复
热议问题