Sort dates and format date in Python - python

I'm working in a code that use date and time. I use .sort() for sort the elements but I have some problems with the date and time format.
List_with_Dict=[
{'Date_Time': '06/12/20 14:1:43', 'Values': ' 46.2'},
{'Date_Time': '06/12/20 13:51:43', 'Values': ' 45.3'},
{'Date_Time': '06/12/20 1:21:47', 'Values': ' 23.0'},
{'Date_Time': '06/12/20 14:17:41', 'Values': ' 46.5'},
{'Date_Time': '06/12/20 13:59:19', 'Values': ' 46.1'},
{'Date_Time': '06/12/20 13:41:43', 'Values': ' 43.9'}]
List_with_Dict.sort(reverse=False, key=lambda e: e['Date_Time'])
for elements in List_with_Dict:
print(elements)
The output is:
{'Date_Time': '06/12/20 13:41:43', 'Values': ' 43.9'}
{'Date_Time': '06/12/20 13:51:43', 'Values': ' 45.3'}
{'Date_Time': '06/12/20 13:59:19', 'Values': ' 46.1'}
{'Date_Time': '06/12/20 14:17:41', 'Values': ' 46.5'}
{'Date_Time': '06/12/20 14:1:43', 'Values': ' 46.2'}
{'Date_Time': '06/12/20 1:21:47', 'Values': ' 23.0'}
As you can see, the two last dictionaries have a different format and can not be sorted. There any solution for this, like a different format date? Or do I need to work in the dictionaries in change the time (h:m:s to hh:mm:ss)?

You can use this for loop to correct the time formats:
List_with_Dict=[
{'Date_Time': '06/12/20 14:1:43', 'Values': ' 46.2'},
{'Date_Time': '06/12/20 13:51:43', 'Values': ' 45.3'},
{'Date_Time': '06/12/20 1:21:47', 'Values': ' 23.0'},
{'Date_Time': '06/12/20 14:17:41', 'Values': ' 46.5'},
{'Date_Time': '06/12/20 13:59:19', 'Values': ' 46.1'},
{'Date_Time': '06/12/20 13:41:43', 'Values': ' 43.9'}]
for d in List_with_Dict:
date, time = d["Date_Time"].split()
time = ':'.join([i.ljust(2, '0') for i in time.split(':')])
d["Date_Time"] = f"{date} {time}"
print(List_with_Dict)
Output:
[{'Date_Time': '06/12/20 14:10:43', 'Values': ' 46.2'},
{'Date_Time': '06/12/20 13:51:43', 'Values': ' 45.3'},
{'Date_Time': '06/12/20 10:21:47', 'Values': ' 23.0'},
{'Date_Time': '06/12/20 14:17:41', 'Values': ' 46.5'},
{'Date_Time': '06/12/20 13:59:19', 'Values': ' 46.1'},
{'Date_Time': '06/12/20 13:41:43', 'Values': ' 43.9'}]
Explanation:
First, iterate through the list of dictionaries:
for d in List_with_Dict:
Get the value of the "Date_Time" key of each dictionary of the iterations, split the values by the space, and assign the
resulting two strings to two variables as the date strings and time strings.
date, time = d["Date_Time"].split()
Split the time string by the colon, pad each time element with 2 "0"s, and join the elements with the colon again.
time = ':'.join([i.ljust(2, '0') for i in time.split(':')])
Reassign the value to the "Date_Time" key of each dictionary, with the converted time string:
d["Date_Time"] = f"{date} {time}"

Related

Python values not getting added to Tuple

Hello I have a situation where I am trying to make a filter in my code customizable so there is minimum duplication of code below is the code that I want to update
for ent in NACLS:
response = ec2.describe_network_acls(Filters=[{'Name': 'network-acl-id', 'Values': [naclID]},
{'Name': 'entry.rule-number', 'Values': [str(ent[0])]},
{'Name': 'entry.protocol', 'Values': [ProtoDict[ent[1]]]},
{'Name': 'entry.port-range.from', 'Values': [str(ent[2])]},
{'Name': 'entry.port-range.to', 'Values': [str(ent[2])]},
{'Name': 'entry.rule-action', 'Values': ["deny"]},
])
I want the filter to customizable for example
for ent in NACLS:
if add = True:
response = ec2.describe_network_acls(Filters=[{'Name': 'network-acl-id', 'Values': [naclID]},
{'Name': 'entry.rule-number', 'Values': [str(ent[0])]},
])
else:
response = ec2.describe_network_acls(Filters=[{'Name': 'network-acl-id', 'Values': [naclID]},
{'Name': 'entry.rule-number', 'Values': [str(ent[0])]},
{'Name': 'entry.protocol', 'Values': [ProtoDict[ent[1]]]},
{'Name': 'entry.port-range.from', 'Values': [str(ent[2])]},
{'Name': 'entry.port-range.to', 'Values': [str(ent[2])]},
{'Name': 'entry.rule-action', 'Values': ["deny"]},
])
This is what I was wanting to do but doesn't work please let me know if there is a better way to achieve this
for ent in NACLS:
filters = {'Name': 'network-acl-id', 'Values': [naclID]}, {'Name': 'entry.rule-number', 'Values': [str(ent[0])]}
filters = filters + tuple({'Name': 'entry.protocol', 'Values': [ProtoDict[ent[1]]]}) //we can add more but this was just to test
print(str(filters)[1:-1])
The output for this is -
{'Name': 'network-acl-id', 'Values': ['acl-08128a2540']}, {'Name': 'entry.rule-number', 'Values': ['80']}, 'Name', 'Values'
When I try to add the value in tuple it shows as blank can someone please guide me on what I am doing wrong here?
I suspect that by adding tuples, you meant to add the dictionary to the tuple of filters, which is not what you are doing in your current script. I suggest that you replace
filters = filters + tuple({'Name': 'entry.protocol', 'Values': [ProtoDict[ent[1]]]})
with
filters = filters + ({'Name': 'entry.protocol', 'Values': [ProtoDict[ent[1]]]},)

Python dictionary not getting updated

Hello I have below code where I am trying to add elements to a dict
for ent in NACLS:
set_filter = {'Name': 'network-acl-id', 'Values': [naclID]}
print(set_filter)
set_filter.update({'Name': 'entry.rule-number', 'Values': [str(ent[0])]})
print(set_filter)
The output for the above code is
{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}
{'Name': 'entry.rule-number', 'Values': ['50']}
{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}
{'Name': 'entry.rule-number', 'Values': ['60']}
{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}
{'Name': 'entry.rule-number', 'Values': ['70']}
Whereas I was wanting it to update the existing dictionary but somehow it's getting overwritten. Expected output
{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}, {'Name': 'entry.rule-number', 'Values': ['50']}
{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}, {'Name': 'entry.rule-number', 'Values': ['60']}
{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}, {'Name': 'entry.rule-number', 'Values': ['70']}
can someone please guide me on how I can achieve this?
The update method adds {key:value} pairs to a dictionary unless the key already exists, in which case it just updates the value to the new value.
From the look of your expected output it seems you want a list of dictionaries all with the same keys but different values.
try this:
set_filter = []
for ent in NACLS:
set_filter.append({'Name': 'network-acl-id', 'Values': [naclID]})
set_filter.append({'Name': 'entry.rule-number', 'Values': [str(ent[0])]})
print(set_filter)
I didn't understand what is the problem here. Do you want to print these dicts like you expected ?
You can write :
for ent in NACLS:
set_filter = {'Name': 'network-acl-id', 'Values': [naclID]}
print(set_filter,end=",")
set_filter.update({'Name': 'entry.rule-number', 'Values': [str(ent[0])]})
print(set_filter,end="\n")
The expected output example isn't a valid dictionary:
{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}, {'Name': 'entry.rule-number', 'Values': ['50']}
Reformatted:
{
'Name': 'network-acl-id',
'Values': [ 'acl-08128a2540eb' ]
},
{
'Name': 'entry.rule-number',
'Values': [ '50' ]
}
To create a list of dictionaries:
for ent in NACLS:
set_filter = [{'Name': 'network-acl-id', 'Values': [naclID]}]
# ^ note square brackets ^
print(set_filter)
set_filter.append({'Name': 'entry.rule-number', 'Values': [str(ent[0])]})
print(set_filter)
Or to put them all into a list:
set_filter = []
for ent in NACLS:
set_filter.append({'Name': 'network-acl-id', 'Values': [naclID]})
print(set_filter)
set_filter.append({'Name': 'entry.rule-number', 'Values': [str(ent[0])]})
print(set_filter)
I don't understand much about your purpose of the list of dictionary, the suggested code
set_filters = []
for ent in NACLS:
set_filters.append({'Name': 'network-acl-id', 'Values': [naclID]})
set_filters.append({'Name': 'entry.rule-number', 'Values': [str(ent[0])]})
is close to your idea. However, the two dictionaries you tried to gather in the list can be combined into just one:
set_filters = []
for ent in NACLS:
set_filters.append(
{
'network-acl-id': [naclID],
'entry.rule-number': [str(ent[0])]
}
)
From here, you can make it shorter as
set_filters = [
{ 'network-acl-id': [naclID], 'entry.rule-number': [str(ent[0])] } for ent in NACLS
]

Python get data with JSON response

I'm making a call to an api which is returning a JSON response, whcih i am then trying to retrieve certain data from within the response.
{'data': {'9674': {'category': 'token',
'contract_address': [{'contract_address': '0x2a3bff78b79a009976eea096a51a948a3dc00e34',
'platform': {'coin': {'id': '1027',
'name': 'Ethereum',
'slug': 'ethereum',
'symbol': 'ETH'},
'name': 'Ethereum'}}],
'date_added': '2021-05-10T00:00:00.000Z',
'date_launched': '2021-05-10T00:00:00.000Z',
'description': 'Wilder World (WILD) is a cryptocurrency '
'launched in 2021and operates on the '
'Ethereum platform. Wilder World has a '
'current supply of 500,000,000 with '
'83,683,300.17 in circulation. The last '
'known price of Wilder World is 2.28165159 '
'USD and is down -6.79 over the last 24 '
'hours. It is currently trading on 21 active '
'market(s) with $2,851,332.76 traded over '
'the last 24 hours. More information can be '
'found at https://www.wilderworld.com/.',
'id': 9674,
'is_hidden': 0,
'logo': 'https://s2.coinmarketcap.com/static/img/coins/64x64/9674.png',
'name': 'Wilder World',
'notice': '',
'platform': {'id': 1027,
'name': 'Ethereum',
'slug': 'ethereum',
'symbol': 'ETH',
'token_address': '0x2a3bff78b79a009976eea096a51a948a3dc00e34'},
'self_reported_circulating_supply': 19000000,
'self_reported_tags': None,
'slug': 'wilder-world',
'subreddit': '',
'symbol': 'WILD',
'tag-groups': ['INDUSTRY',
'CATEGORY',
'INDUSTRY',
'CATEGORY',
'CATEGORY',
'CATEGORY',
'CATEGORY'],
'tag-names': ['VR/AR',
'Collectibles & NFTs',
'Gaming',
'Metaverse',
'Polkastarter',
'Animoca Brands Portfolio',
'SkyVision Capital Portfolio'],
'tags': ['vr-ar',
'collectibles-nfts',
'gaming',
'metaverse',
'polkastarter',
'animoca-brands-portfolio',
'skyvision-capital-portfolio'],
'twitter_username': 'WilderWorld',
'urls': {'announcement': [],
'chat': [],
'explorer': ['https://etherscan.io/token/0x2a3bff78b79a009976eea096a51a948a3dc00e34'],
'facebook': [],
'message_board': ['https://medium.com/#WilderWorld'],
'reddit': [],
'source_code': [],
'technical_doc': [],
'twitter': ['https://twitter.com/WilderWorld'],
'website': ['https://www.wilderworld.com/']}}},
'status': {'credit_count': 1,
'elapsed': 7,
'error_code': 0,
'error_message': None,
'notice': None,
'timestamp': '2022-01-20T21:33:04.832Z'}}
The data i am trying to get is 'logo': 'https://s2.coinmarketcap.com/static/img/coins/64x64/9674.png', but this sits within [data][9674][logo]
But as this script to running in the background for other objects, i won't know what the number [9674] is for other requests.
So is there a way to get that number automatically?
[data] will always be consistent.
Im using this to get the data back
session = Session()
session.headers.update(headers)
response = session.get(url, params=parameters)
pprint.pprint(json.loads(response.text)['data']['9674']['logo'])
You can try this:
session = Session()
session.headers.update(headers)
response = session.get(url, params=parameters)
resp = json.loads(response.text)
pprint.pprint(resp['data'][next(iter(resp['data']))]['logo'])
where next(iter(resp['data'])) - returns first key in resp['data'] dict. In your example it '9674'
With .keys() you get a List of all Keys in a Dictionary.
So you can use keys = json.loads(response.text)['data'].keys() to get the keys in the data-dict.
If you know there is always only one entry in 'data' you could use json.loads(response.text)['data'][keys[0]]['logo']. Otherwise you would need to iterate over all keys in the list and check which one you need.

Python - file.write break loop before finishing

def exportOrders(self):
file = open("orders.txt", 'w')
file.write("\"Date\" \"Pair\" \"Amount bought/sold\" \"Pair Price\" \"Profit/Loss\" \"Order Type\"" + '\n')
for x in self.tradeHistory:
date = x['date']
pair = self.currentPair
amount = x[self.currentPair]
price = x['price']
order = x['Order Type']
if order == "buy":
spent = x['spent']
file.write(date + ' ' + pair + ' ' + amount + ' '
+ price + ' ' + float(-spent) + ' ' + order + ' \n')
if order == "sell":
obtained = x['obtained']
file.write(date + ' ' + pair + ' ' + amount + ' '
+ price + ' ' + obtained + ' ' + order + ' \n')
file.close()
self.tradeHistory is a list of dictionaries that store a date, a pair, the amount bought, the price of the pair, the money spent or obtained, and the order type.
My problem is that when the program runs for the first time into:
if order == "buy":
spent = x['spent']
file.write(date + ' ' + pair + ' ' + amount + ' '
+ price + ' ' + str(float(-spent)) + ' ' + order + ' \n')
The for loop breaks out and the orders.txt only shows the first line which is:
file.write("\"Date\" \"Pair\" \"Amount bought/sold\" \"Pair Price\" \"Profit/Loss\" \"Order Type\"" + '\n')
Thank you in advance!
edit:
Basically, my self.tradeHistory has the following content
{'date': 1505161800, 'BTC_ETH': 0.7091196761422075, 'price': 0.07050996, 'spent': 0.05, 'Order Type': 'buy'}
{'date': 1505167200, 'BTC_ETH': 0.7091196761422075, 'price': 0.07079909, 'obtained': 0.050205027771963, 'Order Type': 'sell'}
{'date': 1505236500, 'BTC_ETH': 0.7032346826344071, 'price': 0.07110002, 'spent': 0.05, 'Order Type': 'buy'}
{'date': 1505251800, 'BTC_ETH': 0.7032346826344071, 'price': 0.0707705, 'obtained': 0.04976827010737831, 'Order Type': 'sell'}
{'date': 1505680200, 'BTC_ETH': 0.715374411944349, 'price': 0.06989347, 'spent': 0.05, 'Order Type': 'buy'}
{'date': 1505699100, 'BTC_ETH': 0.715374411944349, 'price': 0.071989, 'obtained': 0.05149908854146174, 'Order Type': 'sell'}
{'date': 1505733300, 'BTC_ETH': 0.6879187705515734, 'price': 0.072683, 'spent': 0.05, 'Order Type': 'buy'}
{'date': 1505745000, 'BTC_ETH': 0.6889021311187427, 'price': 0.07257925, 'spent': 0.05, 'Order Type': 'buy'}
{'date': 1505756700, 'BTC_ETH': 1.3768209016703161, 'price': 0.0732, 'obtained': 0.10078329000226714, 'Order Type': 'sell'}
...
There are 63 items inside the list of dictionaries. My aim is to create a .txt file that looks like
"Date" "Pair" "Amount bought/sold" "Pair Price" "Profit/Loss" "Order Type"
1505161800 BTC_ETH 0.7091196761422075 0.07050996 0.05 buy
1505167200 BTC_ETH 0.7091196761422075 0.07079909 0.05 sell
...
You should not concatenate numbers with strings in Python. Use str.format instead:
file.write(
'{} {} {} {} {} {}\n'
.format(date, pair, amount, price, float(-spent), order)
)
You can also use csv module for a better implementation.
import csv
def exportOrders(self):
with open("orders.txt", 'w') as file:
writer = csv.writer(file, delimiter=' ', quotechar='"')
writer.writerow([
'Date', 'Pair', 'Amount bought/sold', 'Pair Price',
'Profit/Loss', 'Order Type'])
for x in self.tradeHistory:
date = x['date']
pair = self.currentPair
amount = x[self.currentPair]
price = x['price']
order = x['Order Type']
if order == "buy":
spent = x['spent']
writer.writerow([
date, pair, amount, price,
float(-spent), order])
if order == "sell":
obtained = x['obtained']
writer.writerow([
date, pair, amount, price,
obtained, order])

Filtering and rearranging very large dictionary array without pandas

I have a very large dictionary array that looks like this:
masterArray =[{'value': '-1', 'product': 'product1', 'Customer': 'customer1',
'Sensor': 'sensor1', 'Date': '20170302', 'type': 'type1', 'ID': '100'},
{'value': '20', 'product': 'product1', 'Customer': 'customer1',
'Sensor': 'sensor1','Date': '20170302', 'type': 'type2', 'ID': '100'},
{'value': '0', 'product': 'product1', 'Customer': 'customer1',
'Sensor': 'sensor1', 'Date': '20170302', 'type': 'type1', 'ID': '101'},
{'value': '-5', 'product': 'product1', 'Customer': 'customer1',
'Sensor': 'sensor1', 'Date': '20170302', 'type': 'type2', 'ID': '101'}]
I need to be able to print out individual csvs for each day, product, sensor, and customer, with the first column as the ID #s and the types as the rest of the columns with the value as the data filled in the rows.
ID, type1, type2
100, -1, 20
101, 0, -5
I also created a date set and a 'combination' set to gather unique dates and combinations of product, sensor, and customer.
Unfortunately I am not allowed to get the pandas library installed, although I think what I want to do would be done by this:
df = pd.DataFrame(masterArray)
df.head()
pivot = pd.pivot_table(df, index=['ID'], values=['value'], columns=['type'])
for date in dateset:
#filter for date
pqd = pivot.query('Date == date')
for row in comboset:
#filter for each output
pqc = pqd.query('Customer == row[0] & product == row[1] & sensor == row[2]')
outputName = str(row[0] + '_' + date + '_' + row[1] + '_' + row[2] + '.csv')
filepath = os.path.join(path, outputName)
pqc.to_csv(filepath) #print
Currently my pandas-less idea is changing my masterArray into a huge nested dictionary (I create masterArray myself from other input csv files) but I am not sure if this is the most efficient way or not. I also don't know how best to set up the logic for that large of a nested dictionary. Please help!
You can probably try something like this:
data_dict = {}
for each in masterArray:
if not data_dict.has_key(each['ID']):
data_dict[each['ID']] = []
data_dict[each['ID']].append({each['type']: each['value']})

Categories

Resources