Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何读取excel单元格中的文本,并使用python在json输出中替换为其他值?_Json_Python 3.x_Xlrd - Fatal编程技术网

如何读取excel单元格中的文本,并使用python在json输出中替换为其他值?

如何读取excel单元格中的文本,并使用python在json输出中替换为其他值?,json,python-3.x,xlrd,Json,Python 3.x,Xlrd,我的python代码读取excel工作表并将其转换为json文件输出。我在excel工作表中有一列,其中的值要么是计划内的,要么是计划外的 1在json输出中,我希望计划内的替换为1,计划外的替换为2,而不改变excel文件中的任何内容。 2在输出中,我不希望出现数据。 3在excel中,我的开始时间列值如下2018-11-16 08:00:00。我希望输出为2018-11-16T08:00:00Z。目前我得到了一些垃圾值。 下面是我的代码 import xlrd, json, time, py

我的python代码读取excel工作表并将其转换为json文件输出。我在excel工作表中有一列,其中的值要么是计划内的,要么是计划外的

1在json输出中,我希望计划内的替换为1,计划外的替换为2,而不改变excel文件中的任何内容。 2在输出中,我不希望出现数据。 3在excel中,我的开始时间列值如下2018-11-16 08:00:00。我希望输出为2018-11-16T08:00:00Z。目前我得到了一些垃圾值。 下面是我的代码

import xlrd, json, time, pytz, requests
from os import sys
from datetime import datetime, timedelta
from collections import OrderedDict

def json_from_excel():
    excel_file = 'test.xlsx'
    jsonfile = open('ExceltoJSON.json', 'w')
    data = []
    datestr = str(datetime.now().date())
    loaddata = OrderedDict()

    workbook = xlrd.open_workbook(excel_file)
    worksheet = workbook.sheet_by_name('OMS-GX Data Extraction')
    sheet = workbook.sheet_by_index(0)

for j in range(0, 6):
    for i in range(1, 40):
        temp = {}
        temp["requestedStart"] = (sheet.cell_value(i,0))      #Start Time
        temp["requestedComplete"] = (sheet.cell_value(i, 1))  #End Time
        temp["location"] = (sheet.cell_value(i, 3))           #Station
        temp["equipment"] = (sheet.cell_value(i, 4))          #Device Name
        temp["switchOrderTypeID"] = (sheet.cell_value(i, 5))  #Outage Type
        data.append(temp)
        loaddata['data'] = data



    json.dump(loaddata, jsonfile, indent=3, sort_keys=False)
    jsonfile.write('\n')
    return loaddata



 if __name__ == '__main__':
    data = json_from_excel()
以下是我的示例输出:

 {
   "data": [
      {
         "requestedStart": testtime,
         "requestedComplete": testtime,
         "location": "testlocation",
         "equipment": "testequipment",
         "switchOrderTypeID": "Planned"
      },
      {
         "requestedStart": testtime,
         "requestedComplete": testtime,
         "location": "testlocation",
         "equipment": "testequipment",
         "switchOrderTypeID": "Unplanned"
      }
   ]
}

对第一个问题的答复: 您可以使用条件赋值

如果sheet.cell_值i,则临时[switchOrderTypeID]=1,5==0

对第二个问题的答复: 使用loaddata=data,它将是一个json数组,不使用数据作为json键

对第三个问题的答复:

从dateutil.parser导入解析 t=2018-11-16 08:00:00
parset.strftime%Y-%m-%dT%H:%m:%SZ

感谢它的工作。你能帮我做第二个吗?试试loaddata=data。如果可以的话,它将是一个json数组。因为目前,数据是一个json键,它保存了其余的数据。或者让我知道预期的输出格式。我添加了第三个问题。请您也帮帮忙好吗?关于第三个问题的答案,请告诉我如何更改这里的行,temp[requestedStart]=sheet.cell\u valuei,0用您的单元格值替换t。temp[requestedStart]=parsesheet.cell_valuei,0.strftime%Y-%m-%dT%H:%m:%SZ