简体   繁体   English

Python:如何以特定格式在文件中写入数据?

[英]Python: How to write data in file in specific format?

i have an array called MAC1_Val: 我有一个名为MAC1_Val的数组:

MAC1_Val array([ 1.00000000e+00, -1.00000000e+01, -2.06306600e+02, 2.22635749e+02, 1.00000000e+00, 1.00000000e+01, 1.00000000e+01, -2.06306600e+02, 2.22635749e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00, -1.08892735e+01, 1.88607749e+01, 1.03153300e+01, -1.78666757e+01, 3.33333333e-07, -3.33333333e-07, -4.21637021e-05, 4.21637021e-05, 9.98844400e-01, -1.73973001e-03, 1.20938900e-03, 1.87742948e-03, -3.33333333e-03, 6.66666667e-03, -3.33333333e-03, -2.64911064e-01, -2.60959501e+01, 2.81614422e+01, 3.33333333e-03, -6.66666667e-03, 3.33333333e-03, 0.00000000e+00, 0.00000000e+00]) MAC1_Val数组([1.00000000e + 00,-1.00000000e + 01,-2.06306600e + 02,2.22635749e + 02,1.00000000e + 00,1.00000000e + 01,1.00000000e + 01,-2.06306600e + 02,2.22635749e + 02、0.00000000e + 00、0.00000000e + 00、0.00000000e + 00、0.00000000e + 00、0.00000000e + 00、0.00000000e + 00、0.00000000e + 00、0.00000000e + 00、0.00000000e + 00、0.00000000e + 00,0.00000000e + 00,0.00000000e + 00,0.00000000e + 00,0.00000000e + 00,0.00000000e + 00,0.00000000e + 00,0.00000000e + 00,1.00000000e + 00,-1.08892735e + 01,1.88607749e +01,1.03153300e + 01,-1.78666757e + 01,3.33333333e-07,-3.33333333e-07,-4.21637021e-05,4.21637021e-05,9.98844400e-01,-1.73973001e-03,1.20938900e- 03,1.87742948e-03,-3.33333333e-03,6.66666667e-03,-3.33333333e-03,-2.64911064e-01,-2.60959501e + 01,2.81614422e + 01,3.33333333e-03,-6.66666667e- 03,3.33333333e-03,0.00000000e + 00,0.00000000e + 00])

and i want to write in file (.txt) values in specific format like this: 我想以这种特定格式写入文件(.txt)值:

1.000000e+00
-1.000000e+01 
-2.063066e+02
2.226357e+02
1.000000e+00   
1.000000e+01 .......

note that are 6 digits behind floating point 请注意,浮点数后面有6位数字

any suggestions how to do this? 任何建议如何做到这一点? thanks in advance! 提前致谢!

printf standard is your friend: printf standard是您的朋友:

for i in MAC1_Val:
    print "%.6e" % i

1.000000e+00
-1.000000e+01
-2.063066e+02
2.226357e+02
1.000000e+00
1.000000e+01
1.000000e+01

使用字符串插值来格式化数字。

'%.3f' % (1.23456,)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中具有特定格式的文件中写入不同的字符串 - how to write different string in a file with specific format in python 以特定格式写入数据输出python - write data output in specific format python 如何以表格格式将干净数据写入python文件中 - How to write clean data to a file in python in tabulated format 如何格式化列表数据并写入 selenium python 中的 csv 文件? - How to format list data and write to csv file in selenium python? 如何将数据转换为所需格式并写入文件 - Python + Apache Beam - How to convert data to desired format and write to a file - Python + Apache Beam 如何在具有特定格式和标题的文件中写入字典? - How to write dict in file with specific format and headers? 如何在具有特定格式的文件中写入列表? - how to write a list in a file with a specific format? 如何以特定格式写入文本文件 - How to write to a text file in a specific format 如何使用特定的输出格式在python中将矩阵(从一个文件读取)写入另一个csv文件 - How to write a matrix (read from one file) to another csv file in python with a specific output format 使用 Python 将特定格式的数据保存到文件中 - Saving data in a specific format to a file using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM