【问题标题】:Problem in creating multiple json files in python for loop在python for循环中创建多个json文件的问题
【发布时间】:2020-06-26 11:53:34
【问题描述】:

我正在尝试在 for 循环中创建多个 json 文件,我在下面尝试,但下面出现错误

OSError: [Errno 22] Invalid argument: 'c:\\csv\x0bolume_current_{vol_size}.json'

我尝试了什么:

storage_info =({"VolumeType": vol_type,"Size": vol_size,"InstanceId": inst_id,"Encrypted": encryption_status,"AliasName": alias_name})
for s in storage_info:
      filename = 'c:\csv\volume_current_{vol_size}.json'
      with open(filename, "w") as f:
         json.dump(storage_info, f, indent=4, separators=(',', ': '))

当我在主循环中获得多个卷大小值(例如 8、2)时,我需要为每个大小创建一个 json 作为一个文件..像 volumen_current_8.json volume_current_2.json 这样...任何帮助表示赞赏。

【问题讨论】:

    标签: python json file for-loop


    【解决方案1】:

    您没有提到vol_size 的正确键。

    我假设storage_info 为多个listlist

    你可以这样做:

    storage_info = [{"VolumeType": vol_type,"Size": vol_size,"InstanceId": inst_id,"Encrypted": encryption_status,"AliasName": alias_name}, {"VolumeType": vol_type_one,"Size": vol_size_one,"InstanceId": inst_id_one,"Encrypted": encryption_status_one,"AliasName": alias_name_one}]
    
    for s in storage_info:
          filename = 'c:\csv\\volume_current_{}.json'.format(s['Size'])
          with open(filename, "w") as f:
             json.dump(storage_info, f, indent=4, separators=(',', ': '))
    

    如果您的storage_info 本身就是dict,那么您必须为获得的每个storage_info 执行此操作:

    
    storage_info = {"VolumeType": vol_type,"Size": vol_size,"InstanceId": inst_id,"Encrypted": encryption_status,"AliasName": alias_name}
    
    filename = 'c:\csv\\volume_current_{}.json'.format(storage_info['Size'])
    with open(filename, "w") as f:
        json.dump(storage_info, f, indent=4, separators=(',', ': '))
    

    【讨论】:

    • 我收到filename = 'c:\csv\volume_current_{}.json'.format(s['Size']) TypeError: string indices must be integers 错误
    • 每次循环我都会得到这样的 storage_info `{'VolumeType': 'gp', 'Size': 2, 'InstanceId': 'xxxxxxxxxxx', 'Encrypted': True, 'AliasName ': 'EBS'}
    • s 是 {'VolumeType': 'gp', 'Size': 2, 'InstanceId': 'xxxxxxxxxxx', 'Encrypted': True, 'AliasName': 'EBS'}。对吗?
    • 是的。每次迭代都会像上面一样一一获取所有卷
    • 试过了,得到相同的OSError: [Errno 22] Invalid argument: 'c:\\csv\x0bolume_current_8.json' 。呵呵...它似乎无法形成路径..不知道为什么:(我在windows中运行它
    【解决方案2】:

    问题在于filename 变量。该变量对于字符串是静态的。您可以简单地使用 f-string literels 来解决问题。

    只需将filename = 'c:\csv\volume_current_{vol_size}.json' 更改为filename = f'c:\csv\volume_current_{vol_size}.json'

    一切正常。

    完整代码

    storage_info =({"VolumeType": vol_type,"Size": vol_size,"InstanceId": inst_id,"Encrypted": encryption_status,"AliasName": alias_name})
    for s in storage_info:
          filename = f'c:\csv\volume_current_{vol_size}.json'
          with open(filename, "w") as f:
             json.dump(storage_info, f, indent=4, separators=(',', ': '))
    

    【讨论】:

    • 尝试得到同样的错误..它没有正确替换文件路径:(
    • c:\csvolume_current_8.json 当我尝试打印文件名时,路径中的 \ 似乎有问题
    • 变量vol_size的值是多少?您是否已声明或从storage_info 获得它?因为如果您从存储在变量storage_info 中的字典中获取它,filename = f'c:\csv\volume_current_{s['vol_size']}.json' 应该可以工作。
    • 您的文件名是如何从 'c:\\csv\x0bolume_current_{vol_size}.json' 更改为 c:\csvolume_current_8.json。预期的行为是'c:\\csv\volume_current_8.json'
    • 路径中的附加 \ 修复了问题filename = f'c:\csv\\volume_current_{vol_size}.json'
    【解决方案3】:

    我现在解决了这个问题,我在下面的路径中添加了一个额外的 \。不确定是否正确的方式现在认为路径是正确的

    for s in storage_info:
        filename = f'c:\csv\\volume_current_{vol_size}.json'
        print(filename)
        with open(filename, "w") as f:
            json.dump(storage_info, f, indent=4, separators=(',', ': '))
    

    编辑2:

    将 \ 替换为 / 在它工作的路径中(一些学习我应该使用 /)

    filename = f'c:\csv\\volume_current_{vol_size}.json' 
    to   
    filename = f'c:/csv/volume_current_{vol_size}.json'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 2015-10-23
      • 2021-08-03
      • 1970-01-01
      • 2013-07-21
      • 2022-01-10
      • 2020-09-13
      • 1970-01-01
      相关资源
      最近更新 更多