My answers are below in red. On Fri, Jun 19, 2015 at 2:47 PM, Joonas Liik <liik.joo...@gmail.com> wrote:
> > You say you are taking this from an xml file and want to get a CSV file.. > > Why are you making an intermediate JSON file? > I thought that this would be the most efficient method as it emulates Python dictionaries. In addition the json module has numerous functional purposes. > Why do you need the CSV output? > Client requirements Could you perhaps be better off using another format? > Perhaps...not sure if it worth revisiting at this point. > > Your data seems to be a quite deeply nested hierarchical structure and > doesn't > seem to suit the simple CSV format very well.. (i see several layers of > nested arrays for one).. > how to you plan to map these nested structures to the CSV format? > > That's my greatest challenge. From what I understand, I need to replicate each key and associate it with n number of items in the array. I have been able to address by denesting by one level using this function: 1. def flatten(d, parent_key=''): 2. items = [] 3. for k, v in d.items(): 4. try: 5. items.extend(flatten(v, '%s%s_' % (parent_key, k)).items()) 6. except AttributeError: 7. items.append(('%s%s' % (parent_key, k), v)) 8. return dict(items) 9. 10. final = (flatten(data2, parent_key ='')) For example: 1. data2 = { 2. 3. "OTF": "0", 4. "F": "False", 5. "F": { 6. "Int32": ["0", 7. "0", 8. "0", 9. "0", 10. "0", 11. "0", 12. "0", 13. "0", 14. "0", 15. "0", 16. "0", 17. "0", 18. "0", 19. "0", 20. "0", 21. "0", 22. "0", 23. "0", 24. "0", 25. "0", 26. "0", 27. "0", 28. "0", 29. "0", 30. "0"] 31. }, 32. "D": { 33. "B": ["0", 34. "0", 35. "0", 36. "0", 37. "0", 38. "0", 39. "0", 40. "0", 41. "0", 42. "0", 43. "0"] 44. }, 45. 46. "PBDS": { 47. "DateTime": ["1/1/0001 12:00:00 AM", 48. "1/1/0001 12:00:00 AM", 49. "1/1/0001 12:00:00 AM", 50. "1/1/0001 12:00:00 AM", 51. "1/1/0001 12:00:00 AM", 52. "1/1/0001 12:00:00 AM", 53. "1/1/0001 12:00:00 AM", 54. "1/1/0001 12:00:00 AM"] 55. }, 56. 57. "PBDS": { 58. "Double": ["0", 59. "0", 60. "0", 61. "0", 62. "0", 63. "0", 64. "0", 65. "0"] 66. }, 67. 68. "SCS": { 69. "String": ["1", 70. "2"] 71. } 72. 73. } is turned into 1. {'D_B': ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], 2. 'F_Int32': ['0', 3. '0', 4. '0', 5. '0', 6. '0', 7. '0', 8. '0', 9. '0', 10. '0', 11. '0', 12. '0', 13. '0', 14. '0', 15. '0', 16. '0', 17. '0', 18. '0', 19. '0', 20. '0', 21. '0', 22. '0', 23. '0', 24. '0', 25. '0', 26. '0'], 27. 'OTF': '0', 28. 'PBDS_Double': ['0', '0', '0', '0', '0', '0', '0', '0'], 29. 'SCS_String': ['1', '2']} I would need an output that maps each key with each item in the array. DB1 : 0, DB2: 0, DB3: 0 etc. and F1: 0, F1: 0. DB1, DB2 would be the headers and the 0s as values in the CSV file. > And your example json bit.. the JSON you posted in your last post seems > quite different from some of the first ones you posted. > > You cant expect to give people 5% of a malformed simplified example and to > get anything useful (much less usable) back. > > And finally, the original xml? mock at least? > All of our XML from our client is malformed. > > > > > -- > https://mail.python.org/mailman/listinfo/python-list > >
-- https://mail.python.org/mailman/listinfo/python-list