第一步:运行.py文件生成json文件
问题一:json.decoder.JSONDecodeError: Invalid \escape: line 1 column 31616 (char 31615)
解决:
for dataset in dataset_list:
with open(datasetmap[dataset] + ".csv", "r") as lines:
for i, line in enumerate(lines):
if i == 0:
continue
print(f"Processing line {i}: {line.strip()}")
fid, _, label = re.split(',|\.', line.strip())
label = label.replace('\n', '')
if label not in filelists[dataset]:
folderlist.append(label)
filelists[dataset][label] = []
print(f"Checking directory: {os.path.join(data_path, label)}")
# 检查路径是否存在
if not exists(os.path.join(data_path, label)):
print(f"Directory does not exist: {join(data_path, label)}")
continue
fnames = listdir(os.path.join(data_path, label))
print(f"Found fnames: {fnames}")
fname_number = [int(re.split('_|\.', fname)[1]) for fname in fnames if re.split('_|\.', fname)[1].isdigit()]
sorted_fnames = [fname for fname, _ in sorted(zip(fnames, fname_number), key=lambda f_tuple: f_tuple[1])]
print(f"Sorted fnames: {sorted_fnames}")
if fid[-5:].isdigit():
fid = int(fid[-5:]) - 1
else:
print(f"Invalid fid: {fid}")
continue
print("Adjusted fid:", fid)
if fid < 0 or fid >= len(sorted_fnames):
print(f"Index {fid} is out of range for sorted_fnames")
continue
fname = join(data_path, label, sorted_fnames[fid])
print(f"Appending filename: {fname}")
filelists[dataset][label].append(fname)
for key, filelist in filelists[dataset].items():
cl += 1
random.shuffle(filelist)
filelists_flat[dataset] += filelist
labellists_flat[dataset] += np.repeat(cl, len(filelist)).tolist()
问题二:OSError: [Errno 22] Invalid argument: 'E:\\桌面文件\\Pair-wise-Similarity-module-master\\filelists\\miniImagenet\\train\n07584110\n07584110_9665.JPEG'
解决:\n是转义字符,会导致路径识别错误,我要把json文件中所有的 \ 变成 \\ 。
# 假设json文件路径为
json_file_path = 'E:/code/Pair-wise-Similarity-module-master/filelists\miniImagenet/val.json'
# 读取JSON文件
with open(json_file_path, 'r', encoding='GB2312') as f:
data = json.load(f)
# 将所有的单斜杠`\`替换成双反斜杠`\\`
modified_data = json.dumps(data).replace('\\', '\\\\')
# 写回到JSON文件中
with open(json_file_path, 'w', encoding='utf-8') as f:
f.write(modified_data)
print(f"JSON文件 {json_file_path} 中的内容已经修改完成,所有的单斜杠'\\'都被替换成了双反斜杠'\\\\'。")
问题三:SyntaxError: Non-UTF-8 code starting with '\xbc' in file E:\桌面文件\Pair-wise-Similarity-module-master\filelists\miniImagenet\json.py on line 6, but no encoding declared
解决:文件是其他编码类型,需要先测试出文件编码类型,然后修改
encoding='GB2312'
import chardet
# 检测文件是什么类型的编码
with open('E:/code/Pair-wise-Similarity-module-master/filelists\miniImagenet/val.json', 'rb') as f:
result = chardet.detect(f.read()) # 读取一定量的数据进行编码检测
print(result['encoding']) # 打印检测到的编码