【问题标题】:Python create .txt with the information in json filePython 使用 json 文件中的信息创建 .txt
【发布时间】:2021-11-22 00:04:35
【问题描述】:

大家好,我是 python 新手。现在我得到了一个 json 文件

{
  "file_version": "1",
  "version_data": "4197",
  "panel_info": {
    "panel_barcode": "J Type:  GGA   Stype:  All",
    "board_barcode": [
      "NoSerialNumber"
    ],
    "inspection_time": "2021-08-24-20-07-10",
    "machine": "GGB",
    "model_name": "AIC",
  },

json还有很长的列表,我就跳过它。 我想读取json文件并创建一个名称为(GGB[$]J Type[#] GGA Stype[#] All[$]NoSerialNumber.txt)的txt文件,里面的txt是空的。

我也想循环,因为每个文件夹中有很多这些 json 文件,很难手动逐个创建。

这是我的代码(目前我只是尝试创建一个 txt 1st)

import json

input_path = "C:\Data\Data1\AI.json"
output_path = "C:\index"

with open(input_path) as f:
  data = json.load(f)

  model_name = data['panel_info']['model_name']
  panel_barcode = data['panel_info']['panel_barcode']
  board_barcode = data['panel_info']['board_barcode']

  txt_name = model_name + '[$]' + panel_barcode + '[$]'
  f.close()

  output = open(output_path + '\\'+ (txt_name) + '.txt' , 'w')
  output.close()

输出

OSError: [Errno 22] Invalid argument: 'C:\\index\\GGB[$]J Type:  GGA   Stype:  All[$].txt'

可以帮助检查我的代码是否正确以及如何将 : 符号更改为 [#] 因为窗口无法使用 : 创建名称

【问题讨论】:

  • 嗨,Yzj,你在努力什么?你自己尝试过吗?请输入代码,显示您所做的事情..
  • 您至少需要展示一些代码,请注意SO 不是免费的代码编写服务。
  • 对不起。刚刚添加了我的代码,但不知道如何将 : 更改为 [#]

标签: python json txt


【解决方案1】:

json.dump(json_data, open("file_name_of_choice", "w))

【讨论】:

    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    • 2014-04-21
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多