I'm struggling to bring my array to the right format for the JSON file! Might be an easy question for people with knowledge.
I grep the data from different CSV files in the format:
[['Dec', '196610'], ['Oct', '196699'], ['Sep', '131073'], ['Jul', '122050']]
I need to bring it in the format:
{
"Year_2021": [
{
"Dec": "196610",
"oct": "196699",
"Sep": "131073",
"Jul": "122050"
}
]
}
i have tried different possibilities and got the craziest constellations, just not the right one.
My current code:
import csv
import datetime
import time
import os.path
import json
from collections import defaultdict
sys_time = datetime.datetime.now()
format_sys_time = sys_time.strftime('[%Y-%m-%d] %H:%M:%S') # Other Datetime Format
act_month = int(sys_time.strftime('%m'))
#print act_month
output_json_file = "/var/www/json/EXP_MONTHLY_POWER_" + sys_time.strftime('%Y') + ".json"
start_time = time.time() # measure how long a programm run takes
columns = defaultdict(list) # each value in each column is appended to a list
# Help function to build dword
def word_to_dword(val_1, val_2):
result = val_1
result |= val_2 << 16
return result
# Calculate past days of the current month
dates = []
for act_month in range(act_month+1, 1, -1):
act_month -= 1
if act_month <=9:
INPUTFILE= "/var/www/csv/EXP_POWER_" + sys_time.strftime('%Y-0') + str(act_month) + ".csv"
else:
INPUTFILE= "/var/www/csv/EXP_POWER_" + sys_time.strftime('%Y-') + str(act_month) + ".csv"
dates.append(INPUTFILE)
#print(dates)
outputfields = [];
#Delete old file to avoid double entrys
if os.path.exists(output_json_file):
os.remove(output_json_file)
for dayfile in dates:
if os.path.exists(dayfile):
with open(dayfile) as csvdatei:
mvg_reader = csv.DictReader(csvdatei , delimiter=';') # read rows into a dictionary format
for row in mvg_reader: # read a row as {column1: value1, column2: value2,...}
for (fieldname, value) in row.items(): # go over each column name and value
columns[fieldname].append(value) # append the value into the appropriate list
# based on column name
outfields1=[]
for value in columns['Power_Value(kwh)']:
# print(int(value))
outfields1.append(int(value))
# Build Power Value from last values of list
if (len(outfields1) != 0):
DL_Actual_Power_Value = outfields1[-1]
else:
print("Keine Daten gefunden!")
compString = ""
compString = dayfile[28:30] # Build compString from Filename
switcher = {
"01": "Jan",
"02": "Feb",
"03": "Mar",
"04": "Apr",
"05": "May",
"06": "Jun",
"07": "Jul",
"08": "Aug",
"09": "Sep",
"10": "Oct",
"11": "Nov",
"12": "Dec"
}
outputfields.append([switcher.get(compString, "Invalid month"), str(DL_Actual_Power_Value)] )
print(outputfields)
export_json_main = {}
export_json_main.clear()
export_KEY = 'Year_' + sys_time.strftime('%Y') # Name des Keys festlegen
export_json_main[export_KEY] = []
export_json_main[export_KEY].append( outputfields )
#jsonStr = json.dumps(outputfields)
#print(jsonStr)
#for keys in (outputfields):
# print(keys[0])
#export_json_main[export_KEY].append({
# json.dumps(outputfields)
#})
with open(output_json_file, 'w') as outfile:
json.dump(export_json_main, outfile, indent=4)
This brings me the following output:
{
"Year_2021": [
[
[
"Dec",
"196610"
],
[
"Oct",
"196699"
],
[
"Sep",
"131073"
],
[
"Jul",
"122050"
]
]
]
}
not sure what your code does. However you mean something like this
arr = [['Dec', '196610'], ['Oct', '196699'], ['Sep', '131073'], ['Jul', '122050']]
d = {}
for a in arr:
d[a[0]] = a[1]
result = { "Year_2021": d }
?
Related
I have a program that compares values from the database and from a CSV file. My program works like this.
Database has values.
User uploads a file (multiple users multiple
files).
The program compares the values from the database and the
CSV files and gives an output.
Which tells me that this particular value was found in this user's file.
But I want the program to show me that if the value was found in the other user's file or not.
Here is a working example.
DB Values = [1,23,33,445,6656,88]
Example values of the CSV files:
File 1 values = [1,23,445,77,66,556,54]
File 2 values = [1,23,45,77,366]
File 3 values = [1,23,5,77,5356,524]
Output needed:
{'1':[(user1, some value),(user2, some value)...]}
Here my code:
def LCR(request):
template = "LCR\LCRGen.html"
dest = Destination.objects.values_list('dest_num', flat=True)
ratelist = { }
csv_file = { }
data_set = { }
io_string = { }
vendor = RateFile.objects.values_list()
v_count = vendor.count()
for v_id, v_name, v_file in vendor:
vendor_name = str(v_name)
vendornames = str(v_name)
vendornames = { }
for desNum in dest:
desNum = str(desNum)
for countvar in range(v_count):
csv_file[vendor_name] = RateFile.objects.get(id=v_id).ven_file
data_set[vendor_name] = csv_file[vendor_name].read().decode("UTF-8")
io_string[vendor_name] = io.StringIO(data_set[vendor_name])
next(io_string[vendor_name])
for column in csv.reader(io_string[vendor_name], delimiter=str(u",")):
vendornames[column[0]] = column[1]
for venNum, venValue in vendornames.items():
venlen = len(venNum)
deslen = len(desNum)
if venlen >= deslen or venlen <= deslen:
if desNum[:-1] == venNum[:-1] and desNum[:-2] == venNum[:-2] and desNum[:-3] == venNum[:-3]:
ratelist[desNum] = [(vendor_name, venValue),]
if (vendor_name, venValue) in ratelist[desNum]:
ratelist[desNum] = [
(vendor_name, venValue),]
elif desNum[:-1] == venNum[:-2] and desNum[:-2] == venNum[:-3] and desNum[:-3] == venNum[:-4]:
ratelist[desNum] = [(vendor_name, venValue),]
if (vendor_name, venValue) in ratelist[desNum]:
ratelist[desNum] = [
(vendor_name, venValue),]
elif desNum[:-1] == desNum[:-3] and desNum[:-2] == venNum[:-4] and desNum[:-3] == venNum[:-5]:
ratelist[desNum] = [(vendor_name, venValue),]
elif desNum[:-1] == venNum[:-5] and desNum[:-2] == venNum[:-6]:
ratelist[desNum] = [(vendor_name, venValue),]
if (vendor_name, venValue) in ratelist[desNum]:
ratelist[desNum] = [
(vendor_name, venValue),]
else:
pass
print ( ratelist )
return render ( request, template, { "ratelist" : ratelist } )
Output
Zong, Tata are usernames and the float values is their respective value for the key value of the dictionary.
{'12': [('Zong', ' 0.026')], '213': [('Tata', ' 4.150')], '49': [('Tata', ' 0.531')], '30': [('Zong', ' 0.87')], '454': [('Tata', ' 0.531')], '374': [('Zong', ' 0.87')],
This is what you asked for:
### your data example
db = [1,23,33,445,66,556,88]
us1 = [1,23,445,77,66,556,54]
us2 = [1,23,45,77,366]
### create a list of usernames (to use the string name in dictionary)
userlist = [ "us1", "us2" ]
### intialize the dict for results
values_dict = {}
### open the loop on DB values
for value in db :
# open loop on userlist
for user in userlist :
# if value is found in user list of values
if value in eval(user) :
# if values still NOT a key of results dictionary create the key with the tuple list as values
if value not in values_dict :
values_dict.update({ value : [ ( user, value ) ] })
# else just append the tuple (username, value) to the results dictionary for the DB value corresponding key
else :
values_dict[value].append([ ( user, value ) ])
values_dict
### OUTPUT:
{1: [('us1', 1), [('us2', 1)]], 23: [('us1', 23), [('us2', 23)]], 445: [('us1', 445)], 66: [('us1', 66)], 556: [('us1', 556)]}
but it makes no sense cause it simply check if a value is in the user list of values and add a tuple just to confirm it, it doesn't require all this code, could be simplified a lot. But I'm thinking that I misunderstood your question (please review the english), probably you need to use the DB value as the key to retrieve another value from the user...please review and update
I am building a bot game for my friends in LINE. I'm a beginning coder. I'm trying to call an object in json which includes a string + integer. I've looked around but nothing seems to fit what I need. What would be the best/simple solution?
My code is amateur, please go easy on me. :P
I'm trying to have Python extract through Json, "Name" + "Stat".
Right now it only extracts "Name" and randomly selects an item. Is there any way to select the item + the stat, display the item and calculate the stat? Thanks.
Python 3:
if text == 'FIGHT':
with open('items.json', 'r') as f:
data = json.load(f)
armor1 = [v for d in data['armor'] for k,v in d.items() if k == 'name']
weapon1 = [v for d in data['weapon'] for k,v in d.items() if k == 'name']
magic1 = [v for d in data['magic'] for k,v in d.items() if k == 'name']
armor2 = random.choice(armor1)
weapon2 = random.choice(weapon1)
magic2 = random.choice(magic1)
calc = add(int(armor2), int(weapon2), int(magic2))
line_bot_api.reply_message(
event.reply_token,
TextSendMessage('Armor = ' + (armor2)),
TextSendMessage('Weapon = ' + (weapon2)),
TextSendMessage('Magic = ' + (magic2)),
TextSendMessage('You have a score of ' + str(calc) + '.'),
TextSendMessage('Waiting for next opponent...')
)
Json:
"armor": [
{
"name":"Cardboard armor 10 DEF" ,
"stats":"10" },
{
"name":"Plastic armor 20 DEF" ,
"stats":"20" },
{
"name":"Rubber armor 30 DEF" ,
"stats":"30" },
{
"name":"Metal armor 40 DEF" ,
"stats":"40" },
{
"name":"Indestructable armor 50 DEF" ,
"stats":"50" }
],
After trying just about everything.. The solution was:
if text == 'FIGHT':
with open('items.json', 'r') as f:
data = json.load(f)
armor2 = random.choice(data['armor'])
weapon2 = random.choice(data['weapon'])
magic2 = random.choice(data['magic'])
calc = add(armor2['stats'], weapon2['stats'], magic2['stats'])
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage('Armor = ' + (armor2['name'])),
TextSendMessage('Weapon = ' + (weapon2['name'])),
TextSendMessage('Magic = ' + (magic2['name'])),
TextSendMessage('Total = ' + str(calc) + '.')
]
)
Thanks to everyone and special thanks to my friend Sae who helped me. :)
I am writing a code in python to count how many of which type error codes are occurred in the file
My file :
Tue Sep 01 23:50:17 2015|132491151|959796398631|95931002712|Tue Sep 1 23:50:17 2015|Tue Sep 1 23:50:17 2015|CMT|Undelivered|none|Submit|0|SMSC_no_error|SMPP|ESMEGW|16 Bit|39|78|no||no|no||None|No|NO|no|no|0|0|0|0||||0|0||0|no|no|default_billing|-1|0|no|no|1|1|0|0|1|1|0|0||78456|ussdt|78456|Wed Sep 02 23:50:17 2015|SR|mptcdma2_r|87690|IN|012463814411280170009|||0|1541843||0|0||0|||||||||||||||||||||0|
But I am not getting proper output. Where I am missing. Here is my code
#!/usr/bin/python
import sys
import xlsxwriter
import subprocess
import pprint
from addict import Dict
data = Dict()
if len(sys.argv) == 2:
date = sys.argv[1]
print date
else:
date = subprocess.check_output("date --date='01 hour ago' +%y%m%d%H",shell=True)
date=date.rstrip()
print date
input_dir = "/root/prac/Harini/CDR/"
result_dir = "/root/prac/Harini"
year = "20"+date[0:2]
month = date[2:4]
day = date[4:6]
hour = date[6:8]
db_date = year+"-"+month+"-"+day
array = ();
files = ();
try:
files = subprocess.check_output("ls " + input_dir + "SMSCDR_POSTPAID_" + date + "*.log", stderr=subprocess.STDOUT,shell=True)
array = files.splitlines()
except:
print "No files in the directory"
for single in array:
fp=open(single,"r")
string = fp.readlines();
for line in string:
array = line.split('|')
exp = array[7]
system_id = array[55]
last_failure = array[11]
assign_short_code = array[54];
if (exp == "Expired" or exp == "Undelivered"):
data.db_date.hour.system_id.assign_short_code.last_failure = data[db_date][hour][system_id][assign_short_code].setdefault(last_failure, 0)+1;
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(data)
Output:
{ '2016-04-08': { '11': { 'ussdt': { '78456': { 'SMSC_no_error': 0}}}},
'db_date': { 'hour': { 'system_id': { 'assign_short_code': { 'last_failure': 1}}}}}
Expected output
{ '2016-04-08': { '11': { 'ussdt': { '78456': { 'SMSC_no_error': 1}}}},
You're getting that db_date key because you're adding it:
data.db_date.hour.system_id.assign_short_code.last_failure = data[db_date][hour][system_id][assign_short_code].setdefault(last_failure, 0)+1;
# ^^^^^^^
You are using addict. In standard dict notation, that line is equivalent to this:
data['db_date']['hour']['system_id']['assign_short_code']['last_failure'] = data[db_date][hour][system_id][assign_short_code].setdefault(last_failure, 0)+1;
# ^^^^^^^^^
I think you meant to write this:
data[db_date][hour][system_id][assign_short_code][last_failure] = data[db_date][hour][system_id][assign_short_code].setdefault(last_failure, 0) + 1
That is, you want to use the value of db_date (and other variables), not the literal string 'db_value'.
This:
data.db_date.hour.system_id.assign_short_code.last_failure = data[db_date][hour][system_id][assign_short_code].setdefault(last_failure, 0)+1;
should be
data[db_date][hour][system_id][assign_short_code].setdefault(last_failure, 0)
data[db_date][hour][system_id][assign_short_code] = data[db_date][hour][system_id][assign_short_code] + 1
Note: in python no semicolons
I am trying to read through a csv file in the following format:
number,alphabet
1,a
2,b
3,c
2,b
1,a
My code to create a dictionary:
alpha = open('alpha.csv','r')
csv_alpha = csv.reader(alpha)
alpha_file = {row[0]:row[1] for row in csv_alpha}
OUTPUT:
alpha_file = { 1:'a', 2:'b', 3:'c' }
By looking at the file, 1 and 2 have duplicate values.
How can i possibly change my output to :
alpha_file = { 1:'a', 1:'a', 2:'b', 2:'b', 3:'c' }
LNG - PYTHON
use a list to hold key's value
alpha = open('alpha.csv','r')
csv_alpha = csv.reader(alpha)
alpha_file = dict()
for row in csv_alpha:
if row[0] in alpha_file:
alpha_file[row[0]].append(row[1])
else:
alpha_file[row[0]] = [row[1]]
the output will be like:
{ 1:['a','a'],2:['b','b'], 3:['c'] }
to output the number of key occurrences, use a for loop
d = { 1:['a','a'],2:['b','b'], 3:['c'] }
amount = []
for key, value in d.iteritems():
amount += [key] * len(value)
print amount
output looks like:
[1, 1, 2, 2, 3]
I have a JSON file with numerous entries like this:
{
"area1": "California",
"area2": "Sierra Eastside",
"area3": "Bishop Area",
"area4": "Volcanic Tablelands (Happy/Sad Boulders)",
"area5": "Fish Slough Boulders",
"grade": "V6 ",
"route": "The Orgasm",
"type1": "Boulder",
"type2": "NONE",
"type3": "NONE",
"type4": "NONE",
},
I want to take the area and type entries and turn them into arrays:
{
"area": ["California","Sierra Eastside","Bishop Area","Volcanic Tablelands (Happy/Sad Boulders)","Fish Slough Boulders"]
"grade": "V6 ",
"route": "The Orgasm",
"type": ["Boulder","NONE","NONE","NONE"]
},
I have this code which almost works:
json_data=open('../json/routes_test.json')
datas = json.load(json_data)
datas_arrays = []
area_keys = ['area1','area2','area3','area4','area5']
type_keys = ['type1','type2','type3','type4']
for data in datas:
areaArray = []
typeArray = []
deleteArray = []
for k, v in data.iteritems():
for area_key in area_keys:
if (k == area_key):
areaArray.append(v)
deleteArray.append(k)
for type_key in type_keys:
if (k == type_key):
typeArray.append(v)
deleteArray.append(k)
for k in deleteArray:
del data[k]
data['area'] = areaArray
data['type'] = typeArray
datas_arrays.append(data)
print datas_arrays
print "********"
out = json.dumps(datas_arrays, sort_keys=True,indent=4, separators=(',', ': '))
print out
f_out= open('../json/toues_test_intoarrays.json', 'w')
f_out.write(out)
f_out.close()
The problem is that the area array is all out of order and the type array is backwards, which I can't have. I find it strange that one is unordered and one is ordered but backwards. To me it seems like the iteration should assure they're placed in order.
Python dictionaries have an arbitrary ordering, they are not sorted. You want to use your prebuilt lists of keys instead:
with open('../json/routes_test.json') as json_data:
datas = json.load(json_data)
area_keys = ['area1','area2','area3','area4','area5']
type_keys = ['type1','type2','type3','type4']
for data in datas:
data['area'] = [data[k] for k in area_keys]
data['type'] = [data[k] for k in type_keys]
for k in area_keys + type_keys:
del data[k]
out = json.dumps(datas, sort_keys=True, indent=4, separators=(',', ': '))
print out
with open('../json/toues_test_intoarrays.json', 'w') as f_out:
f_out.write(out)
which changes the dictionaries in-place.
You could even determine the area and type keys from each entry:
for data in datas:
keys = sorted(data.keys())
area_keys = [k for k in keys if k.startswith('area')]
data['area'] = [data[k] for k in area_keys]
type_keys = [k for k in keys if k.startswith('type')]
data['type'] = [data[k] for k in type_keys]
for k in area_keys + type_keys:
del data[k]
and omit the list literals with the 'area1', 'area2' etc. hardcoded lists altogether.
Iterate the keys in order.
for k, v in sorted(data.iteritems()):
This will fail once you get past 9, but it will do for now.