Reputation: 87
I have a JSON output and I am appending each element into a python list using a for loop. I am using the append
function to add element to my list. Once the loop is completed I want to convert each item in the list into a string. The problem is there each element in the list is another list. So there are for example 2 lists inside one main list. I have tried to exhaust possiblilities of converting the list into strings using the code below.
I have tried using :
print '\n'.join(map(str, folder_concat))
print '\n'.join(str(x) for x in folder_concat)
I also tried to convert the appended list before appending by using:
''.join(map(str, my_string.append(...elements to append...)
but, the output is always the same unicode format in the typical list style as follows:
(u'190430', u'TEST', u'Executing', False, False, u'20190430000522', u'20190430000522', u'20190506141820')
(u'190430', u'TEST1', u'Executing', False, False, u'20190430000522', u'20190430000522', u'20190506141820')
To add to the request some of the elements that are populated in the append are lists as well for example estimatedStartTime
and this can be fetched with empty data so I cant iterate over it using (str(x) for x in folder_concat)
as it will fail with the Iterable Error
.
Here is the script I am using:
#!/usr/bin/python
import json
import sys
json_string = sys.stdin.read()
json_data = json.loads(json_string)
returned = json_data['returned']
if returned > 0:
locations = json_data['statuses']
sess_count = 0
folder_count = 0
folder_concat = []
folder_str = []
for i in locations:
if i['type'] == 'Folder':
folder_count += 1
folder_concat.append((
i.get('orderDate', ''),
i.get('folderId', ''),
i.get('status', ''),
i.get('held', ''),
i.get('deleted', ''),
i.get('startTime', ''),
(''.join(i.get('estimatedStartTime', ''
)[0]) if 'estimatedStartTime'
in i else ''.join(i.get('estimatedStartTime', ''))),
(''.join(i.get('estimatedEndTime', ''
)[0]) if 'estimatedEndTime'
in i else ''.join(i.get('estimatedEndTime', ''))),
))
else:
pass
print '\n'.join(str(x) for x in folder_concat)
elif odata['returned'] == 0:
print 'No results fetched.'
else:
pass
The input file is:
{
"statuses" : [ {
"orderDate" : "190430",
"folderId" : "TEST",
"status" : "Executing",
"held" : false,
"deleted" : false,
"startTime" : "20190501000551",
"estimatedStartTime" : [ "20190501000551" ],
"estimatedEndTime" : [ "20190505043236" ],
} ,{
"orderDate" : "190430",
"folderId" : "TEST1",
"status" : "Executing",
"held" : false,
"deleted" : false,
"startTime" : "20190501000551",
"estimatedStartTime" : [ "20190501000551" ],
"estimatedEndTime" : [ "20190505043236" ],
}],
"returned" : 2,
"total" : 2
}
The results should look like:
190430, TEST, Executing, False, False, 20190430000522, 20190430000522, 20190506141820
190430, TEST1, Executing, False, False, 20190430000522, 20190430000522, 20190506141820
Upvotes: 0
Views: 75
Reputation: 151
First, when you do str on a tuple, it will print exactly the tuple, so with the parenthesis.
I would change the folder output by something like this:
for i in locations:
folder_count += 1
content = [
i.get('orderDate', ''),
i.get('folderId', ''),
i.get('status', ''),
i.get('held', ''),
i.get('deleted', ''),
i.get('startTime', ''),
(''.join(i.get('estimatedStartTime', ''
)[0]) if 'estimatedStartTime'
in i else ''.join(i.get('estimatedStartTime', ''))),
(''.join(i.get('estimatedEndTime', ''
)[0]) if 'estimatedEndTime'
in i else ''.join(i.get('estimatedEndTime', ''))),
]
folder_concat.append(', '.join(str(item) for item in content))
print '\n'.join(str(x) for x in folder_concat)
This way, you have a first step in order to transform your element as the wanted string, and then you keep the same flow. Let me know if you have any question.
Upvotes: 1