python2.7中typeerror: long() argument must be a string or a number,not 'NoneType'该怎么解决

import cv2
import glob, os.path
import gzip
import numpy as np
import caffe

caffe.set_mode_cpu() # running on gpu mode
TMP_PATH = "./TMP/" # temp folder where landsat bands will be extracted
model_def = './deepwatermap3_deploy.prototxt' # model description file
model_weights = './deepwatermap3.caffemodel' # trained model file './rivanet_iter_460351.caffemodel'
d = './p001r062_WC_20010731' # path to the directory where landsat bands are stored

def im2double(I):
""" Converts image datatype to float """
if I.dtype == 'uint8':
I = I.astype('float')/255

if I.dtype == 'uint16':
I = I.astype('float')/65535
return I

def mndwi(green, mir):
green = im2double(green)
mir = im2double(mir)

numerator = green-mir
denominator = green+mir
numerator[denominator==0] = 0
denominator[denominator==0] = 1

mndwi = numerator / denominator

return mndwi

MU = np.asarray([69, 57, 59, 59, 64, 46], dtype='float32')
bands = ("_10.tif.gz", "_20.tif.gz", "_30.tif.gz", "_40.tif.gz", "_50.tif.gz", "_70.tif.gz")

I_all = None

# add bands
for i in range(0, len(bands)):
input_band_path = glob.glob(d + '/*' + bands[i])[0]

# extract the landsat archives into a temporary folder
temp_dir = TMP_PATH
if not os.path.exists(temp_dir):
os.makedirs(temp_dir)

temp_im_dir = os.path.join(temp_dir, os.path.basename(input_band_path)[:-3])

with gzip.open(input_band_path, 'rb') as in_file:
s = in_file.read()
with open(temp_im_dir, 'w') as out_file:
out_file.write(s)

# open temp_im_dir, load it as a channel in I
I_band = cv2.imread(temp_im_dir, cv2.IMREAD_UNCHANGED)

if I_all is None:
R, C = I_band.shape
I_all = np.zeros((R, C, 6), 'uint8')

I_all[:,:,i] = I_band

# delete the temp file
os.remove(temp_im_dir)
其中 I_all[:,:,i] = I_band一直出错

出现这种情况一般都是获取内容时得到的是json格式数据或者说是字典格式数据,然后把json格式数据写入文件时才会报错。解决方法如下:

1、首先在Python中可以调用json模块以处理json格式数据,这样json格式数据便可以转换成字符串了。

2、如图,转换方法就是dumps,然后把转换后的字符串赋值给aa即可。

3、这样执行的时候就不会报错了,但是这样直接写入文件会出现\u乱码,这就涉及到编码问题了。

4、所以要在dumps里面添加一个参数,ensure_ascii,并且设置为FALSE选项才可以。

5、最后再次运行即可把字典的内容成功的写入到文件当中去了。

温馨提示:答案为网友推荐,仅供参考
相似回答