写一个数组到磁盘蟒蛇 [英] writing an array to disk in python

查看:111
本文介绍了写一个数组到磁盘蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用蟒蛇基本上作为一个计算器,从端子间preTER 。但是,对于一个特定的工作,我需要把它写成.py文件并将其结果保存到文件中。

有关我的真正的问题,我想出了code是:

 #least.py程序
导入numpy的是NP
从scipy.optimize进口curve_fit
扩展数据= np.array([0.1639534,0.2411005,0.3130353,0.3788510,0.4381247,0.5373147,0.6135673,0.6716365,0.7506711,0.8000908,0.9000000])
YDATA = np.array ([7.1257999E-04,9.6610998E-04,1.1894000E-03,1.3777000E-03,1.5285000E-03,1.7297000E-03,1.8226000E-03,1.8422999E-03,1.7741000E-03,1.6574000E-03,1.1877000E-03])高清FUNC(X,A,B,C):
    返回A + B * X + C * X ** 3
POPT,pcov = curve_fit(FUNC,XDATA,YDATA,P0 =(1,1,1))

和尝试将它们写在磁盘上。

如从终端,POPT的值,pcov将简单地通过提供

 >>> POPT
阵列([-5.20906980e-05,4.41458412e-03,-3.65246935e-03])

我试图把它写在磁盘上,追加least.py作为(给出<一个href=\"http://stackoverflow.com/questions/3685265/how-to-write-a-multidimensional-array-to-a-text-file\">here):

 与文件(3fit','W')为OUTFILE:
    outfile.write(POPT)

这给了我错误:

 回溯(最后最近一次调用):
  文件least.py,9号线,上述&lt;&模块GT;
    与文件(3fit','W')为OUTFILE:
NameError:名称'文件'是没有定义

请帮助。
我在Linux机器上,使用python 3.3

 打印(内容sys.version)
3.3.5(默认情况下,2014年3月10日,3时21分31秒)
[GCC 4.8.2 20140206($ P $租赁前)

修改
我想在一列的数据,如:

  -5.20906980e-05
 4.41458412e-03
-3.65246935e-03


解决方案

您使用的是Python3,其中文件()不是一个函数了。使用的open()代替。

此外,你可以只写字符串。那么,如何你想有 POPT 重新psented作为一个字符串完全$ P $?如果你想获得相同的输出在控制台上,再版()会做什么:

 开放('3fit','W')为OUTFILE:
    outfile.write(再版(POPT))

或者你可以只写的数值,以空格分隔:

 开放('3fit','W')为OUTFILE:
    outfile.write(''。加入(STR(VAL)为VAL在POPT))

I use python basically as a calculator, from terminal interpreter. But, for a particular work, I need to write it as .py file and save its result to a file.

For my real problem, the code I came up with is:

#least.py program
import numpy as np
from scipy.optimize import curve_fit
xdata = np.array([0.1639534, 0.2411005, 0.3130353, 0.3788510,  0.4381247, 0.5373147, 0.6135673, 0.6716365, 0.7506711,  0.8000908, 0.9000000])
ydata =np.array ([7.1257999E-04,9.6610998E-04,1.1894000E-03,1.3777000E-03,1.5285000E-03,1.7297000E-03,1.8226000E-03,1.8422999E-03,1.7741000E-03,1.6574000E-03,1.1877000E-03])

def func (x,a,b,c):
    return a+b*x+c*x**3
popt, pcov =curve_fit(func,xdata,ydata,p0=(1,1,1))

and trying to write them in disk.

As from terminal, the value of popt, pcov will simply available by:

>>> popt
array([ -5.20906980e-05,   4.41458412e-03,  -3.65246935e-03])

I tried to write it in disk, appending the least.py as(as given here):

with file('3fit','w') as outfile:
    outfile.write(popt)

which gives me error:

Traceback (most recent call last):
  File "least.py", line 9, in <module>
    with file('3fit','w') as outfile:
NameError: name 'file' is not defined

Kindly help. I am in linux machine, using python 3.3

print (sys.version)
3.3.5 (default, Mar 10 2014, 03:21:31) 
[GCC 4.8.2 20140206 (prerelease)]

EDIT I would like those data in a column,as:

-5.20906980e-05   
 4.41458412e-03  
-3.65246935e-03

解决方案

You are using Python3, where file() is not a function any more. Use open() instead.

Furthermore, you can only write strings. So how do you want to have popt represented as a string exactly? If you want to get the same output as on the console, repr() will do:

with open('3fit', 'w') as outfile:
    outfile.write(repr(popt))

Or you could just write the numerical values, separated by blanks:

with open('3fit', 'w') as outfile:
    outfile.write(' '.join(str(val) for val in popt))

这篇关于写一个数组到磁盘蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆