将Python输出放入.xlxs文件

将Python输出放入.xlxs文件,python,Python,下面的代码运行并执行它应该执行的操作,但是我在Python中使用XlxsWriter模块时遇到了问题,无法将一些结果放入.xlxs文件中。目标是让输出包含来自功能块\u试验的信息,其中它跟踪每个块,并为我提供与该试验对应的all\u powers变量。将模块安装到我的用户目录中进展顺利,但它不会给我一个同时提供两组信息的文件 目前,我正在使用: import xlsxwriter workbook = xlsxwriter.Workbook('SRT_data.xlsx') worksheet

下面的代码运行并执行它应该执行的操作,但是我在Python中使用XlxsWriter模块时遇到了问题,无法将一些结果放入.xlxs文件中。目标是让输出包含来自功能块\u试验的信息,其中它跟踪每个块,并为我提供与该试验对应的all\u 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
导入随机、数学
num_特征=20
stim_to_vect={}
所有刺激=[1,2,3,4,5]
所有特征=范围(数量特征)
零=[0表示所有刺激中的i]
内存=[]
def snoc(xs,x):
new_xs=xs.copy()
新的_xs.append(x)
返回新的
def concat(xss):
新的_xs=[]
对于xss中的xs:
新的扩展(xs)
返回新的
def point_wise_mul(xs,ys):
返回[x*y代表x,y在zip中(xs,ys)]
对于snoc中的s(所有时间,0):
刺激向量[s]=[]
对于所有_功能中的i:
stim_to_vect[s].append(random.choice([-1,1]))
def相似性(x,y):
返回(math.fsum(point_-wise_-mul(x,y))/math.sqrt(math.fsum(point_-wise_-mul(x,x))*math.fsum(point_-wise_-mul(y,y)))
def回波(探头、电源):
回声向量=[]
对于所有功能中的j:
总数=0
对于范围内的i(len(内存)):
总+=math.pow(相似性(探测,内存[i]),功率)*内存[i][j]
回显向量追加(总计)
回波向量
固定顺序=[1,5,3,4,2,1,3,5,4,2,5,1]
上个州={}
上一个州[0]=[]
prev=0
对于固定汇率:
如果当前不在上一状态。键():
上一个州[当前]=[]
上一个状态[curr]。追加(上一个)
上一次=当前
def更新_存储器(学习_参数,事件):
memory.append([i if random.random()0.97:
电流响应=第二部分(theEcho)
返回功率,电流响应
返回10,零
def块_试验(顺序):
所有的权力=[]
prev_resp=刺激到向量[0]
对于顺序中的i:
curr\u stim=stim\u to\u vect[i]
电源,电流响应=比较(电流刺激,上一次响应)
更新内存(0.7,concat([curr\u stim,prev\u resp,curr\u resp]))
所有权力。附加(权力)
prev_resp=curr_resp
归还所有的权力

您可以添加代码中的一部分,在其中您可以尝试将想要的内容输出到excel文件中吗?block_试用功能。其想法是创建一个电子表格,用于收集试用编号,然后返回特定试用的all_Power值。我使用openpyxl比xlsxwriter幸运得多。当您运行所有操作时,它会成功吗完成?电子表格是空白的吗?