使用numpy.savetxt后出现的问题

使用numpy.savetxt后出现的问题,numpy,python-2.7,Numpy,Python 2.7,我正在用Python编程。 这是我的代码: def data_exp(nr, nc): data=numpy.zeros((nr, nc)) print data for i in range(0, nr): for j in range (0, nc): data[i, j]=input('Insert values: ') numpy.savetxt(str(input('Insert the name of the f

我正在用Python编程。 这是我的代码:

def data_exp(nr, nc):
    data=numpy.zeros((nr, nc))
    print data
    for i in range(0, nr):
        for j in range (0, nc):
            data[i, j]=input('Insert values: ')
    numpy.savetxt(str(input('Insert the name of the file (ex: "a.txt"): ')), data)
    return data

问题是这个程序什么也不返回!我在numpy.savetxt之后放置的所有内容都被忽略!有人能告诉我如何克服这个问题吗?

你的问题是不正确地使用
输入
input
相当于
eval(原始输入())
eval()
调用将尝试在程序中的全局和局部上下文中对作为python源代码输入的文本进行求值,这显然不是您希望在本例中执行的操作。我很惊讶您没有收到运行时错误报告,您输入的字符串没有定义

尝试使用
raw\u输入

def data_exp(nr, nc):
    data=numpy.zeros((nr, nc))
    print data
    for i in range(0, nr):
        for j in range (0, nc):
            data[i, j]=input('Insert values: ')
    numpy.savetxt(str(raw_input('Insert the name of the file (ex: "a.txt"): ')), data)
    return data

编辑:

这是上面的代码,在ipython会话中为我工作。如果您无法使其正常工作,则会出现其他问题:

In [7]: data_exp(2,2)
[[ 0.  0.]
 [ 0.  0.]]
Insert values: 1
Insert values: 2
Insert values: 3
Insert values: 4
Insert the name of the file (ex: "a.txt"): a.txt
Out[7]: 
array([[ 1.,  2.],
       [ 3.,  4.]])

In [8]: data_exp??
Type:       function
Base Class: <type 'function'>
String Form:    <function data_exp at 0x2ad3070>
Namespace:  Interactive
File:       /Users/talonmies/data_exp.py
Definition: data_exp(nr, nc)
Source:
def data_exp(nr, nc):
    data=numpy.zeros((nr, nc))
    print data
    for i in range(0, nr):
        for j in range (0, nc):
            data[i, j]=input('Insert values: ')
    numpy.savetxt(str(raw_input('Insert the name of the file (ex: "a.txt"): ')), data)
    return data

In [9]: _ip.system("cat a.txt")
1.000000000000000000e+00 2.000000000000000000e+00
3.000000000000000000e+00 4.000000000000000000e+00
[7]中的
data\u exp(2,2)
[[ 0.  0.]
[ 0.  0.]]
插入值:1
插入值:2
插入值:3
插入值:4
插入文件名(例如:“a.txt”):a.txt
出[7]:
数组([[1,2.],
[ 3.,  4.]])
在[8]:数据_exp??
类型:功能
基类:
字符串形式:
名称空间:交互式
文件:/Users/talonmes/data\u exp.py
定义:数据扩展(nr,nc)
资料来源:
def数据表(nr,nc):
数据=numpy.zero((nr,nc))
打印数据
对于范围内的i(0,nr):
对于范围(0,nc)内的j:
数据[i,j]=输入('插入值:')
savetxt(str(原始输入('插入文件名(例如:“a.txt”):')),数据)
返回数据
在[9]中:_ip.system(“cat a.txt”)
1.000000000000000000 E+00 2.000000000000000000 E+00
3.000000000000000000 E+00 4.000000000000000000 E+00

我试过了,但是在numpy.savetxt之后,它仍然没有做任何事情:我要求它做的所有事情都被忽略了!好啊看起来我在python方面有些问题。。。我决定这样做:def data_exp(nr,nc):data=np.zeros((nr,nc))for i in range(0,nr):for j in range(0,nc):data[i,j]=raw_input('Insert value:')print data print'保存数组数字save(data)和Insert name'返回数据def save(data):np.savetxt(str)(input('Insert name of the list)(例如:a.txt):')),数据)这里有一个尝试>>>数组=数据(3,2)插入…(我不会全部写)[[4.3.][2.3.][2.2.]]保存数组数字保存(名称文件)并插入名称。>>保存(数组)插入列表名称“list_1.txt”完成