无法转换'浮动'对象为str隐式[Loggin]

时间:2017-10-16 18:06:37

标签: python

我正在尝试保存一些信息,我收到错误 "无法转换'浮动'反对str的对象“ 我使用str()而不是格式,但它显示相同的错误 请帮助thx。

def save_model(json_string, dirpath='results/'):
    with open(dirpath + starttime +'.txt', 'w') as f:
        f.write(json_string)

def save_config(config, dirpath='results/'):
    with open(dirpath + 'config_log.txt', 'a') as f:
        f.write(starttime + '\n')
        f.write((config) + '\n')

 def save_result(starttime, conv_arch, dense, train_acc, val_acc, dirpath='results/'):
     with open(dirpath + starttime +'_train_val.txt', 'w') as f:
        f.write("{0}".format(train_acc) + '\n')
        f.write("{0}".format(val_acc) + '\n')

     endtime = time.asctime(time.localtime(time.time()))
     with open(dirpath + 'result_log.txt', 'a') as f:
        f.write(starttime + '--' + endtime + ' comment: ' + notes + '\n' )
        f.write("{0}".format(conv_arch) + ',{0}'.format(dense) + '\n')
        f.write('Train acc: {0}'.format(train_acc[-1]) +
            'Val acc: {0}'.format(val_acc[-1]) +
            'Ratio: {0}'.format(val_acc[-1]/train_acc[-1]) + '\n')

这是输出

     INFO - File "cnn6.py", line 109, in cnn_architecture
     INFO - dropout, X_shape, y_shape, train_acc, val_acc, dirpath)
     INFO - File "cnn6.py", line 33, in logging
     INFO - save_result(starttime, conv_arch, dense, train_acc, val_acc, now)
     INFO - File "/code/log.py", line 20, in save_result
     INFO - with open(dirpath + starttime +'_train_val.txt', 'w') as f:
     INFO - TypeError: Can't convert 'float' object to str implicitly

1 个答案:

答案 0 :(得分:1)

这是解决问题所需的。

    F.write(str(starttime)+str(endtime))

原因是您的时间处于浮动状态,您需要通过转换为字符串

进行协调

这是一个基本的tutorial,可以了解有关这些主题的更多信息。