将Python输出放入.xlxs文件中

时间:2015-05-22 05:03:11

标签: python

以下代码运行并执行它应该执行的操作,但我在Python中使用XlxsWriter模块时遇到问题,以便将一些结果转换为.xlxs文件。目标是输出包含来自功能block_trial的信息,它跟踪每个块并给出与该试验对应的all_powers变量。将模块安装到我的用户目录中顺利进行,但它不会给我一个文件,它给我两组信息。

目前,我正在使用:

import xlsxwriter
workbook = xlsxwriter.Workbook('SRT_data.xlsx')
worksheet = workbook.add_worksheet()

加宽第一列以使文字更清晰。

 worksheet.set_column('A:A', 20)

添加粗体格式以用于突出显示单元格。

bold = workbook.add_format({'bold': True})

写一些简单的文字。

worksheet.write('A1', 'RT')
workbook.close() 

但无法显示我的任何数据。

import random, math

num_features = 20
stim_to_vect = {}
all_stim = [1,2,3,4,5]
all_features = range(num_features)
zeros=[0 for i in all_stim]
memory=[]

def snoc(xs,x):
    new_xs=xs.copy()
    new_xs.append(x)
    return new_xs

def concat(xss):
    new_xs = []
    for xs in xss:
        new_xs.extend(xs)
    return new_xs

def point_wise_mul(xs,ys):
    return [x*y for x,y in zip(xs,ys)]

for s in snoc(all_stim, 0):
    stim_to_vect[s]= []
    for i in all_features:
        stim_to_vect[s].append(random.choice([-1, 1]))

def similarity(x,y):
 return(math.fsum(point_wise_mul(x,y))/math.sqrt(math.fsum(point_wise_mul(x,x))*math.fsum(point_wise_mul(y,y))))

def echo(probe,power):
    echo_vect=[]
    for j in all_features:
        total=0
        for i in range(len(memory)):
            total+=math.pow(similarity(probe, memory[i]),power)*memory[i][j]
        echo_vect.append(total)
    return echo_vect

fixed_seq=[1,5,3,4,2,1,3,5,4,2,5,1]
prev_states={}
prev_states[0]=[]
prev=0
for curr in fixed_seq:
    if curr not in prev_states.keys():
        prev_states[curr]=[]
    prev_states[curr].append(prev)
    prev=curr

def update_memory(learning_parameter,event):
    memory.append([i if random.random() <= learning_parameter else 0 for i in event])

for i in snoc(all_stim,0):
    for j in prev_states[i]:
        curr_stim = stim_to_vect[i]
        prev_resp = stim_to_vect[j]
        curr_resp = stim_to_vect[i]
        update_memory(1.0, concat([curr_stim, prev_resp, curr_resp]))

def first_part(x):   
    return x[:2*num_features-1]

def second_part(x):
    return x[2*num_features:]

def compare(curr_stim, prev_resp):
    for power in range(1,10):
        probe=concat([curr_stim,prev_resp,zeros])
        theEcho=echo(probe,power)
        if similarity(first_part(probe),first_part(theEcho))>0.97:
            curr_resp=second_part(theEcho)
            return power,curr_resp
    return 10,zeros

def block_trial(sequence):
    all_powers=[]
    prev_resp = stim_to_vect[0]
    for i in sequence:
        curr_stim = stim_to_vect[i]
        power,curr_resp=compare(curr_stim,prev_resp)
        update_memory(0.7,concat([curr_stim,prev_resp,curr_resp]))
        all_powers.append(power)
        prev_resp=curr_resp
    return all_powers

0 个答案:

没有答案