如何在Python中处理JSON和Windows路径? [英] How to deal with JSON and Windows paths in Python?

查看:84
本文介绍了如何在Python中处理JSON和Windows路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个Python包,该包读取JSON文件(配置文件)以获取信息.

I'm trying to run a Python package which reads a JSON file (config file) for information.

问题是我需要将一些JSON文件编辑到包含的目录中.

The problem is I need to edit some of the JSON file to included directories.

脚本的创建者使用Linux内核,所以他使用Linux路径名,而我使用Windows,这需要在路径名中加反斜杠.

The creator of the script was using a Linux kernel, so he used Linux pathnames, while I'm using Windows, which require backslashes within my pathnames.

这引起了问题. JSON文件的外观如下:

This is causing a problem. Here is the how the JSON file looks:

"train": {
    "train_image_folder":   "C:/Users/Moondra/Desktop/Object Detection/basic-yolo-keras/train_image_folder",
    "train_annot_folder":   "C:/Users/Moondra/Desktop/Object Detection/basic-yolo-keras/train_annot_folder",     

    "train_times":          10,
    "pretrained_weights":   " ",
    "batch_size":           16,
    "learning_rate":        1e-4,
    "nb_epoch":             50,
    "warmup_epochs":        3,

    "object_scale":         5.0 ,
    "no_object_scale":      1.0,
    "coord_scale":          1.0,
    "class_scale":          1.0,

    "saved_weights_name":   "full_yolo_raccoon.h5",
    "debug":                true

},

"valid": {
    "valid_image_folder":   "C:/Users/Moondra/Desktop/Object Detection/basic-yolo-keras/valid_image_folder",
    "valid_annot_folder":   "C:/Users/Moondra/Desktop/Object Detection/basic-yolo-keras/valid_annot_folder",

    "valid_times": 1

}

我找到了这个答案,但是对于我的情况并没有真正的帮助:

I found this answer, but it wasn't really helpful in my case:

要从JSON字符串中删除反斜杠吗?

有没有解决这个问题的简单方法?

Is there an easy way around this?

推荐答案

/是Windows上的有效目录分隔符,自MS-DOS 2起就存在.您甚至可以将它们混合在同一路径中,但是看起来可怕.

/ is a valid directory separator on Windows, and has been since MS-DOS 2. You can even mix them in the same path, but that looks horrible.

某些应用程序坚持使用\,但是您可能需要先尝试使用/,然后才能自己完成工作.

Some applications insist on \, but you might want to try using / before making work for yourself.

在Windows上使用\的重要原因是用户期望的位置. \在Windows文化中已经根深蒂固,以至于人们看到/时都会感到不安.

An important reason for using \ on Windows is where users expect it. The \ has got so ingrained in Windows culture that it unsettles people when they see /.

这篇关于如何在Python中处理JSON和Windows路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆