【问题标题】:Printing floats and integers into a file with given format in python在python中将浮点数和整数打印到具有给定格式的文件中
【发布时间】:2020-06-29 10:45:09
【问题描述】:

我有这个简单的代码:

import numpy as np
from numpy import random
import csv
import os
import pandas as pd

data=np.loadtxt('lums.dat')
data2=np.loadtxt('all.dat')

shuffle=np.random.shuffle(data[0::1])

with open('data', 'w') as f:
 writer = csv.writer(f, delimiter=" ")
 writer.writerows(zip(data2[:,0], data2[:,1], data[:,0], data[:,1], data2[:,4], data2[:,5]))

输出如下所示:

42.9356886 1.0 30.91534 1.0 0.899 1.0
43.6177968 1.0 30.76019 1.0 1.5 1.0
42.6341244 1.0 29.26717 0.0 0.61 1.0

但是,我需要第二、第四和第六列是整数,并且我还希望能够指定第一、第三和第五列的位数。有没有一种简单的方法可以做到这一点?

编辑:文件 lums.dat 包含:

31.2114100 1
33.0060900 1
31.1879700 1
31.1502300 1
30.0178600 1
29.8382100 1

all.dat 文件包含:

42.9356886 1  31.2114100 1  0.89900000 1
43.6177968 1  33.0060900 1  1.50000000 1
42.6341244 1  31.1879700 1  0.61000000 1
42.5828698 1  31.1502300 1  0.69700000 1
42.2789821 1  30.0178600 1  0.26000000 1
41.9161537 1  29.8382100 1  0.21400000 1

【问题讨论】:

  • np.savetxt 允许您指定列格式。
  • loadtxt 在空间上拆分输入行,并将所有值转换为浮点数。你熟悉python%的格式化方法吗?
  • 其实不是,我还是Python的初学者。不过,这个问题已经解决了。非常感谢。

标签: python numpy csv format output


【解决方案1】:

问题是,你不能将类型更改为 int,因为数字在内部仍然是一个数组,所以我使用了dtype。它用于管理np.array 中的类型。舍入数字的工作相同,您必须使用相同原因的 numpy 函数np.round

如果不想使用numpy函数,首先要把变量从数组改成数字。

要格式化特定数字,python 中有%方法。要指定位数,请使用 %.6f 表示 6 位数。在你的情况下,你首先必须处理你的数组格式,我做了一个列表操作。

import numpy as np
import csv

data = np.loadtxt('lums.dat')
data2 = np.loadtxt('all.dat')

shuffle = np.random.shuffle(data[0::1])

with open('data', 'w') as f:
    writer = csv.writer(f, delimiter=" ")
    writer.writerows(zip(np.round(data2[:, 0], 2), np.array(data2[:, 1], dtype=int), np.round(data[:, 0], 2), np.array(data[:, 1], dtype=int), (np.array(["%.8f" % i for i in data2[:, 4]])), np.array(data2[:, 5], dtype=int)))

【讨论】:

  • 实际上我已经尝试过这样的事情,但是我得到了错误:对于 round() 我得到 'TypeError: type numpy.ndarray doesn't define round method',而for int() 'TypeError: 只有大小为 1 的数组可以转换为 Python 标量'。我认为这两个错误都是由于我的数据是 numpy 数组,但我是 Python 新手,不知道如何处理。
  • 我的解决方案有错误吗,如果有,你能证明我的错误信息吗?
  • 能否提供您的数据,以便我重现您的错误
  • 当然,您可以在原始帖子中找到所有数据。基本上,我希望输出文件保持与原始文件相同的格式,每行使用相同的空格/数字等。
  • @Hypernova 我解决了。这是 python 和 Numpy 之间的数据类型的问题。我会更新我的答案。
【解决方案2】:
In [32]: txt="""42.9356886 1  31.2114100 1  0.89900000 1 
    ...: 43.6177968 1  33.0060900 1  1.50000000 1 
    ...: 42.6341244 1  31.1879700 1  0.61000000 1"""                                                                 
In [33]: data = np.loadtxt(txt.splitlines())                                                                         
In [34]: data                                                                                                        
Out[34]: 
array([[42.9356886,  1.       , 31.21141  ,  1.       ,  0.899    ,
         1.       ],
       [43.6177968,  1.       , 33.00609  ,  1.       ,  1.5      ,
         1.       ],
       [42.6341244,  1.       , 31.18797  ,  1.       ,  0.61     ,
         1.       ]])

将其写入指定格式的文件:

In [37]: np.savetxt('test.csv', data, fmt='%.7f %d %.7f %d %.7f %d')                                                 
In [38]: cat test.csv                                                                                                
42.9356886 1 31.2114100 1 0.8990000 1
43.6177968 1 33.0060900 1 1.5000000 1
42.6341244 1 31.1879700 1 0.6100000 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多