简体   繁体   English

将数字写入文件python

[英]Writing numbers into file python

I was trying to extract all the elements of the my data points (x,y) tuples, and put them into list of x values and y list, and transfer them to two columns in excel spreadsheet. 我试图提取数据点(x,y)元组的所有元素,并将它们放入x值和y列表中,然后将它们转移到excel电子表格中的两列中。 It seems writing numbers into file is quite difficult. 将数字写入文件似乎很困难。 Can anyone shed a light on this problem? 谁能阐明这个问题? Current state: 当前状态:

xlist=[list[i][0] for i in range(len(list))] 
ylist=[list[i][1] for i in range(len(list))]
fob=open('c:/test/a.txt','w')
fob.write(xlist[i] for i in range(len(xlist))

i want to write down a column of numbers in notepad so that I can highlight and copy into spread sheet directly . 我想在记事本中写下一列数字,以便可以突出显示并直接复制到电子表格中。 Below are my data. 以下是我的数据。

list = [(0.496, 12.49), (0.531, 12.40), (0.578, 12.18), (0.615, 
   11.96), (0.657, 11.75), (0.731, 11.28), (0.785, 10.85), (0.812, 
   10.61), (0.883, 9.92), (0.930, 9.40), (0.979, 8.77), (1.026, 
   8.10), (1.081, 7.23), (1.134, 6.33), (1.189, 5.39), (1.220, 
   4.85), (1.273, 3.92), (1.332, 2.91), (1.364, 2.55), (1.418, 
   2.16), (1.467, 1.65), (1.523, 1.17), (1.569, 0.82), (1.626, 
   0.47), (1.678, 0.21), (1.723, 0.01), (1.776, 0.19), (1.814, 
   0.28), (1.869, 0.36), (1.933, 0.36), (1.972, 0.31), (2.021, 
   0.18), (2.081, 0.13), (2.129, 0.46), (2.169, 0.79), (2.219, 
   1.24), (2.280, 1.84), (2.306, 2.11), (2.358, 2.67), (2.414, 
   3.37), (2.471, 4.05), (2.505, 4.51), (2.562, 5.22), (2.613, 
   5.84), (2.652, 6.31), (2.712, 7.01), (2.758, 7.52), (2.802, 
   7.99), (2.869, 8.63), (2.930, 9.16), (2.971, 9.57), (3.043, 
   10.35), (3.078, 10.69), (3.119, 11.00), (3.174, 11.26), (3.217, 
   11.40), (3.261, 11.53), (3.307, 11.55), (3.371, 11.51), (3.432, 
   11.40), (3.479, 11.26), (3.507, 11.20), (3.557, 11.00), (3.623, 
   10.55), (3.663, 10.28), (3.729, 9.79), (3.768, 9.57), (3.825, 
   9.24), (3.880, 8.85), (3.944, 8.41), (3.969, 8.04), (4.014, 
   7.55), (4.086, 6.67), (4.105, 6.37), (4.166, 5.50), (4.212, 
   4.88), (4.266, 4.20), (4.311, 3.69), (4.364, 3.06), (4.401, 
   2.65), (4.453, 2.09), (4.497, 1.68), (4.556, 1.18), (4.602, 
   0.85), (4.644, 0.57), (4.695, 0.29), (4.754, 0.04), (4.799, 
   0.11), (4.847, 0.17), (4.918, 0.11), (4.959, 0.04), (4.992, 
   0.19), (5.063, 0.64), (5.098, 0.90), (5.157, 1.40), (5.201, 
   1.79), (5.245, 2.20), (5.291, 2.65), (5.326, 3.00), (5.387, 
   3.65), (5.420, 4.02), (5.469, 4.62), (5.538, 5.44), (5.579, 
   5.96), (5.629, 6.57), (5.674, 7.14), (5.724, 7.73), (5.798, 
   8.60), (5.823, 8.88), (5.888, 9.62), (5.919, 9.94), (5.963, 
   10.41), (6.009, 10.85), (6.050, 11.22), (6.115, 11.71), (6.153, 
   11.99), (6.222, 12.39), (6.263, 12.61), (6.302, 12.77), (6.377, 
   12.99), (6.414, 13.03), (6.454, 13.02), (6.522, 12.89), (6.558, 
   12.74), (6.626, 12.41), (6.677, 12.05), (6.729, 11.64), (6.791, 
   11.00), (6.832, 10.58), (6.887, 9.92), (6.949, 9.13), (6.996, 
   8.48), (7.028, 8.09), (7.094, 7.13), (7.123, 6.70), (7.161, 
   6.16), (7.213, 5.35), (7.250, 4.81), (7.332, 3.61), (7.382, 
   2.93), (7.420, 2.45), (7.474, 1.88), (7.514, 1.40), (7.576, 
   0.71), (7.600, 0.50), (7.662, 0.12), (7.725, 0.16), (7.768, 
   0.26), (7.810, 0.30), (7.858, 0.26), (7.904, 0.18), (7.980, 
   0.10), (8.021, 0.29), (8.078, 0.65), (8.133, 1.06), (8.165, 
   1.33), (8.218, 1.83), (8.267, 2.31), (8.321, 2.87), (8.355, 
   3.27), (8.413, 3.91), (8.473, 4.61), (8.519, 5.22), (8.553, 
   5.65), (8.643, 6.74), (8.678, 7.23), (8.734, 7.94), (8.760, 
   8.27), (8.803, 8.81), (8.851, 9.35), (8.905, 9.94), (8.961, 
   10.45), (9.009, 10.92), (9.053, 11.34), (9.106, 11.75), (9.166, 
   12.14), (9.228, 12.48), (9.292, 12.71), (9.340, 12.86), (9.384, 
   13.01), (9.412, 13.05), (9.452, 13.03), (9.472, 13.00)]

Cheers 干杯

Export it into a CSV file. 将其导出到CSV文件。 Your use case is very simple and you should be able to do it using standard Python. 您的用例非常简单,您应该可以使用标准Python来实现。

with open('output.csv', 'w') as f:
  for x, y in l:
    f.write("%s, %s\n" % (x, y))

Note: list is a reserved word in python and you should not be using it. 注意:list是python中的保留字,您不应该使用它。

Use openpyxl to write .xslx files from Python: 使用openpyxl从Python编写.xslx文件:

import openpyxl

my_list = [(0.496, 12.49), (0.531, 12.40), (0.578, 12.18), (0.615, 
   11.96), (0.657, 11.75), (0.731, 11.28), (0.785, 10.85), (0.812, 
   10.61), (0.883, 9.92), (0.930, 9.40), (0.979, 8.77), (1.026, 
   8.10), (1.081, 7.23), (1.134, 6.33), (1.189, 5.39), (1.220, 
   4.85), (1.273, 3.92), (1.332, 2.91), (1.364, 2.55), (1.418, 
   2.16), (1.467, 1.65), (1.523, 1.17), (1.569, 0.82), (1.626, 
   0.47), (1.678, 0.21), (1.723, 0.01), (1.776, 0.19), (1.814, 
   0.28), (1.869, 0.36), (1.933, 0.36), (1.972, 0.31), (2.021, 
   0.18), (2.081, 0.13), (2.129, 0.46), (2.169, 0.79), (2.219, 
   1.24), (2.280, 1.84), (2.306, 2.11), (2.358, 2.67), (2.414, 
   3.37), (2.471, 4.05), (2.505, 4.51), (2.562, 5.22), (2.613, 
   5.84), (2.652, 6.31), (2.712, 7.01), (2.758, 7.52), (2.802, 
   7.99), (2.869, 8.63), (2.930, 9.16), (2.971, 9.57), (3.043, 
   10.35), (3.078, 10.69), (3.119, 11.00), (3.174, 11.26), (3.217, 
   11.40), (3.261, 11.53), (3.307, 11.55), (3.371, 11.51), (3.432, 
   11.40), (3.479, 11.26), (3.507, 11.20), (3.557, 11.00), (3.623, 
   10.55), (3.663, 10.28), (3.729, 9.79), (3.768, 9.57), (3.825, 
   9.24), (3.880, 8.85), (3.944, 8.41), (3.969, 8.04), (4.014, 
   7.55), (4.086, 6.67), (4.105, 6.37), (4.166, 5.50), (4.212, 
   4.88), (4.266, 4.20), (4.311, 3.69), (4.364, 3.06), (4.401, 
   2.65), (4.453, 2.09), (4.497, 1.68), (4.556, 1.18), (4.602, 
   0.85), (4.644, 0.57), (4.695, 0.29), (4.754, 0.04), (4.799, 
   0.11), (4.847, 0.17), (4.918, 0.11), (4.959, 0.04), (4.992, 
   0.19), (5.063, 0.64), (5.098, 0.90), (5.157, 1.40), (5.201, 
   1.79), (5.245, 2.20), (5.291, 2.65), (5.326, 3.00), (5.387, 
   3.65), (5.420, 4.02), (5.469, 4.62), (5.538, 5.44), (5.579, 
   5.96), (5.629, 6.57), (5.674, 7.14), (5.724, 7.73), (5.798, 
   8.60), (5.823, 8.88), (5.888, 9.62), (5.919, 9.94), (5.963, 
   10.41), (6.009, 10.85), (6.050, 11.22), (6.115, 11.71), (6.153, 
   11.99), (6.222, 12.39), (6.263, 12.61), (6.302, 12.77), (6.377, 
   12.99), (6.414, 13.03), (6.454, 13.02), (6.522, 12.89), (6.558, 
   12.74), (6.626, 12.41), (6.677, 12.05), (6.729, 11.64), (6.791, 
   11.00), (6.832, 10.58), (6.887, 9.92), (6.949, 9.13), (6.996, 
   8.48), (7.028, 8.09), (7.094, 7.13), (7.123, 6.70), (7.161, 
   6.16), (7.213, 5.35), (7.250, 4.81), (7.332, 3.61), (7.382, 
   2.93), (7.420, 2.45), (7.474, 1.88), (7.514, 1.40), (7.576, 
   0.71), (7.600, 0.50), (7.662, 0.12), (7.725, 0.16), (7.768, 
   0.26), (7.810, 0.30), (7.858, 0.26), (7.904, 0.18), (7.980, 
   0.10), (8.021, 0.29), (8.078, 0.65), (8.133, 1.06), (8.165, 
   1.33), (8.218, 1.83), (8.267, 2.31), (8.321, 2.87), (8.355, 
   3.27), (8.413, 3.91), (8.473, 4.61), (8.519, 5.22), (8.553, 
   5.65), (8.643, 6.74), (8.678, 7.23), (8.734, 7.94), (8.760, 
   8.27), (8.803, 8.81), (8.851, 9.35), (8.905, 9.94), (8.961, 
   10.45), (9.009, 10.92), (9.053, 11.34), (9.106, 11.75), (9.166, 
   12.14), (9.228, 12.48), (9.292, 12.71), (9.340, 12.86), (9.384, 
   13.01), (9.412, 13.05), (9.452, 13.03), (9.472, 13.00)]

book = openpyxl.Workbook()
sheet = book.active

for i, value in enumerate(my_list):
    sheet.cell(row=i+1, column=1).value = value[0]
    sheet.cell(row=i+1, column=2).value = value[1]    

book.save('test.xlsx')

When you have data like numbers or objects in memory, it's generally not correct to dump that data directly into disk, you'll want to serialize it. 当内存中有数字或对象之类的数据时,将数据直接转储到磁盘通常是不正确的,您需要对其进行序列化。

The easiest way to serialize it is with print which automatically calls the "serialization" method __str__ . 序列化最简单的方法是使用print,它会自动调用“序列化”方法__str__ The problem with this serialization method is that's not always easy to deserialize. 这种序列化方法的问题在于,反序列化并不总是那么容易。

When you have a data structure, like the matrix you describe, you'll want a serialization method that will preserve the structure and allow to reconstruct it in memory. 当您具有数据结构(如您描述的矩阵)时,您将需要一种序列化方法,该方法将保留该结构并允许在内存中对其进行重构。 In this case you can use CSV (through the csv module), JSON (through the json module) or many others. 在这种情况下,您可以使用CSV(通过csv模块),JSON(通过json模块)或其他许多格式。

Use CSV. 使用CSV。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM