Reputation: 69
'''
import openpyxl
from openpyxl import load_workbook
from openpyxl import Workbook
from Tkinter import Tk
from tkFileDialog import askopenfilename
import itertools
import sys
import warnings
import json
def find (param):
for row in ws.iter_rows():
try:
keyword = row[1].value
values = [item.value for item in row[2:]]
if keyword == ("%s" %(param) ):
keyword = (values)
return keyword
except (AttributeError):
continue
if not sys.warnoptions:
warnings.simplefilter("ignore")
Tk().withdraw()
filename = askopenfilename()
print ("Working, please be patient")
wb = openpyxl.load_workbook(filename, data_only=True)
print ("\r")
count = 0
for ws in wb.worksheets:
tlist = ws.title
print ("%s : %s" %(count,tlist) )
count = count + 1
x = int(input ("\nchoose number: ") )
ws = wb.worksheets[x]
param = "Site Name"
SiteName = find(param)
SiteName = (SiteName)
param = ("NE Name(TID)")
TID = find(param)
TID = (TID)
param = ("Shelf Number")
SH_Num = find(param)
SH_Num = (SH_Num)
param = ("CLLI")
CLLI = find(param)
CLLI = (CLLI)
z = {'SiteName' : SiteName,"TID" : TID, "SH_Num" : SH_Num}
with open('data7.json', 'w') as f:
json.dump(z,f,indent=4)
'''
There are alot more variables that will be in this. Every site has a site name, TID, shelf number, etc.. Wanting to separate the variables by the sites; variables for site 1, variables for site 2, etc.. and access the variables by TID1-SH_Num1[TID]
OUTPUT =
{
"SiteName": [
"Site1",
"Site2",
"Site3",
],
"TID": [
"TID1",
"TID2",
"TID3",
],
"SH_Num": [
"SH_Num1",
"SH_Num2",
"SH_Num3",
]
}
What I am wanting is
{
TID1-SH_Num1: [
"SiteName" : "SiteName1"
"TID": "TID1"
"SH_Num" : "SH_Num1"
],
TID2-SH_Num2: [
"SiteName" : "SiteName2"
"TID": "TID2"
"SH_Num" : "SH_Num2"
],
],
TID3-SH_Num3: [
"SiteName" : "SiteName3"
"TID": "TID3"
"SH_Num" : "SH_Num3"
],
}
etc..
I do have it where the individual sites variables are in separate text files for every site and the variables are all correct per site. Wanting it in JSON now, so I can add all of the sites and variables into tkinter tabs
Upvotes: 1
Views: 82
Reputation: 3450
If you have a dict like this:
a = {
"TID1-SH_Num1": [
"SiteName" : "SiteName1"
"TID": "TID1"
"SH_Num" : "SH_Num1"
],
"TID2-SH_Num2": [
"SiteName" : "SiteName2"
"TID": "TID2"
"SH_Num" : "SH_Num2"
],
],
"TID3-SH_Num3": [
"SiteName" : "SiteName3"
"TID": "TID3"
"SH_Num" : "SH_Num3"
],
}
Then you can just dump it into a file:
import json
with open('data7.json', 'w') as f:
json.dump(a,f,indent=4)
Upvotes: 1