supress scientific notation when writing python floats to files

ε祈祈猫儿з 提交于 2019-12-24 05:13:08

问题


Writing floats to a CSV writes some of them like this: 2.0628800997782577e-05

c = csv.writer(open(file, "wb"))
c.writerow([var1, var2])    

What I've tried:

  • I have already tried var1**8 following other answers on StackOverflow, but this simply raises them to the power of 8.
  • I have also tried Decimal(var1), but this does not suppress the scientific notation.

This makes it difficult to process the output in excel afterwards as it's recognized as text and not a number. How can I print it in non-scientific, decimal notation?


回答1:


'%f' % your_var

Or to control the precision:

'%0.10f' % your_var



回答2:


Use string formatting:

c.writerow(['{:f}'.format(var) for var in (var1, var2)]


来源:https://stackoverflow.com/questions/11605235/supress-scientific-notation-when-writing-python-floats-to-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!