I have an array of Dictionaries in python as shown below, I am iterating over a list and order_number etc is assigned as a variable for each one. I am doing this in AWS Lambda
customer.append({'Name' : name, 'Age': age, 'Order Number': order_number, 'New Customer?': new_customer})
Current the output is as follows:
`[{'Name' : Jason, 'Age': 33, 'Order Number': 234898, 'New Customer?': Yes},{'Name' : Adrian, 'Age': 52, 'Order Number': 387253, 'New Customer?': Yes}, {'Name' : Emily, 'Age': 36, 'Order Number': 103913, 'New Customer?': No}, {'Name' : Imran, 'Age': 24, 'Order Number': 992831, 'New Customer?': No}, ...] and so on.`
I would like the output to be:
[{'Name' : Jason, 'Age': 33, 'Order Number': 234898, 'New Customer?': Yes},
{'Name' : Adrian, 'Age': 52, 'Order Number': 387253, 'New Customer?': Yes},
{'Name' : Emily, 'Age': 36, 'Order Number': 103913, 'New Customer?': No},
{'Name' : Imran, 'Age': 24, 'Order Number': 992831, 'New Customer?': No},
...]
is there a way to do this? I have tried '\n' at a few points however it isn't working as I either get a syntax error or 'EOL while scanning string literal '
Any help appreciated, thanks
CodePudding user response:
You can simply convert entries to strings, join them with newline and print it.
print('\n'.join(map(str, customer)))
Output:
{'Name': 'Jason', 'Age': 33, 'Order Number': 234898, 'New Customer?': 'Yes'}
{'Name': 'Adrian', 'Age': 52, 'Order Number': 387253, 'New Customer?': 'Yes'}
{'Name': 'Emily', 'Age': 36, 'Order Number': 103913, 'New Customer?': 'No'}
{'Name': 'Imran', 'Age': 24, 'Order Number': 992831, 'New Customer?': 'No'}
CodePudding user response:
You can try :
str(customer).replace("}, ", "}, \n")
Output :
[{'Name': 'Jason', 'Age': 33, 'Order Number': 234898, 'New Customer?': 'Yes'},
{'Name': 'Adrian', 'Age': 52, 'Order Number': 387253, 'New Customer?': 'Yes'},
{'Name': 'Emily', 'Age': 36, 'Order Number': 103913, 'New Customer?': 'No'},
{'Name': 'Imran', 'Age': 24, 'Order Number': 992831, 'New Customer?': 'No'}]