繁体   English   中英

写或写二进制文件以将数据保存到python中的文件?

[英]write or write binary for saving data to file in python?

我正在尝试将功能从目录保存到文件中。 数据和错误已显示在下面。 open语句中保存数据而不失真的正确“字母”应该是什么?

错误

write() argument must be str, not numpy.ndarray

generator1 = ImageDataGenerator(rescale=1. / 255).flow_from_directory(train_data_dir,target_size=(img_width, img_height),batch_size=batch_size,shuffle=False)
    print("generator1 images loaded")

    for i in range(len(generator1)):
        kl = generator1[i]
        bottleneck_features_train = model.predict_on_batch(kl[0])
        print("first prediction")
        print (bottleneck_features_train)
        file =open('/home/rehan/predictions/bottleneck_features_train_%s.py'%i, 'w')
        file.write(bottleneck_features_train)
        file.close()

数据

 [[[ 0.50452518  0.          0.         ...,  0.          0.84091663  0.        ]
       [ 0.538715    0.          0.07498804 ...,  0.          0.50906491  0.        ]
       [ 0.5355916   0.          1.27406454 ...,  0.14854321  0.55418521  0.        ]
       [ 1.24407315  0.          1.74664402 ...,  0.11201498  0.55507243  0.        ]]

      [[ 0.05677766  0.          0.         ...,  0.          0.82949859  0.        ]
       [ 0.          0.          0.19032657 ...,  0.12606588  0.02242988  0.        ]
       [ 0.10961182  0.          1.54800272 ...,  0.37970039  0.          0.        ]
       [ 0.42456442  0.          1.87542152 ...,  0.36944041  0.29935738  0.        ]]

      [[ 0.04067653  0.          0.         ...,  0.          0.55476826  0.        ]
       [ 0.31820443  0.          0.         ...,  0.          0.          0.        ]
       [ 0.58587539  0.          0.25692856 ...,  0.03251171  0.          0.        ]
       [ 0.66836131  0.          0.19993514 ...,  0.          0.19460687  0.        ]]

      [[ 0.46838504  0.          0.         ...,  0.          0.91270626  0.        ]
       [ 1.46697009  0.          0.         ...,  0.          0.53989708  0.        ]
       [ 2.26325178  0.          0.         ...,  0.          0.          0.        ]
       [ 1.71381867  0.          0.         ...,  0.          0.34278265  0.        ]]]]

如果要直接将numpy数组写入文件并能够再次加载,则应使用pickle

要编写它:

import pickle

with open("pickle_file.pickle", "wb") as handle:
    pickle.dump(your_array, handle)

阅读:

with open("pickle_file.pickle", "rb") as handle:
    your_array = pickle.load(handle)

将其转换为字符串对象。

例如:

generator1 = ImageDataGenerator(rescale=1. / 255).flow_from_directory(train_data_dir,target_size=(img_width, img_height),batch_size=batch_size,shuffle=False)
    print("generator1 images loaded")

    for i in range(len(generator1)):
        kl = generator1[i]
        bottleneck_features_train = model.predict_on_batch(kl[0])
        print("first prediction")
        print (bottleneck_features_train)
        file =open('/home/rehan/predictions/bottleneck_features_train_%s.py'%i, 'w')
        file.write(str(bottleneck_features_train))
        file.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM