Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {
  7. "collapsed": true
  8. },
  9. "outputs": [],
  10. "source": [
  11. "import csv\n",
  12. "import json\n",
  13. "\n",
  14. "classes = {'0':'car', '1':'bike'}\n",
  15. "\n",
  16. "final_output = [\n",
  17. " [\n",
  18. " 'filename1',\n",
  19. " 250,\n",
  20. " {'class': classes['0'], 'attr':['a', 'b', 'c']},\n",
  21. " ['hello', 'man'],\n",
  22. " 5\n",
  23. " ],\n",
  24. " [\n",
  25. " 'filename2',\n",
  26. " 355,\n",
  27. " {'class': classes['1'], 'attr':['c', 'd', 'a']},\n",
  28. " ['howdy', 'man'],\n",
  29. " 10\n",
  30. " ]\n",
  31. "]\n",
  32. "y = ['filename', 'file_size', 'file_attributes', 'some_array', 'region_count']\n",
  33. "\n",
  34. "with open('file.csv', 'w', newline='') as f:\n",
  35. " wr = csv.writer(f, delimiter=';')\n",
  36. " wr.writerow(y)\n",
  37. " for output in final_output:\n",
  38. " jsonified_output = [json.dumps(o) if isinstance(o, dict) or isinstance(o, list) else o for o in output]\n",
  39. " wr.writerow(jsonified_output)\n"
  40. ]
  41. },
  42. {
  43. "cell_type": "markdown",
  44. "metadata": {},
  45. "source": [
  46. "## csv file's content would look like this\n",
  47. "\n",
  48. "```\n",
  49. "filename;file_size;file_attributes;some_array;region_count\n",
  50. "filename1;250;\"{\"\"class\"\": \"\"car\"\", \"\"attr\"\": [\"\"a\"\", \"\"b\"\", \"\"c\"\"]}\";\"[\"\"hello\"\", \"\"man\"\"]\";5\n",
  51. "filename2;355;\"{\"\"class\"\": \"\"bike\"\", \"\"attr\"\": [\"\"c\"\", \"\"d\"\", \"\"a\"\"]}\";\"[\"\"howdy\"\", \"\"man\"\"]\";10\n",
  52. "```"
  53. ]
  54. },
  55. {
  56. "cell_type": "code",
  57. "execution_count": 2,
  58. "metadata": {},
  59. "outputs": [
  60. {
  61. "name": "stdout",
  62. "output_type": "stream",
  63. "text": [
  64. "car\n",
  65. "hello\n",
  66. "bike\n",
  67. "howdy\n"
  68. ]
  69. }
  70. ],
  71. "source": [
  72. "with open('file.csv', 'r') as csv_file:\n",
  73. " rd = csv.reader(csv_file, delimiter=';')\n",
  74. " next(rd, None) # skip the header row\n",
  75. " for row in rd:\n",
  76. " file_attributes = row[2]\n",
  77. " fileattr_dict = json.loads(file_attributes)\n",
  78. " print(fileattr_dict['class'])\n",
  79. " arr = row[3]\n",
  80. " arr_list = json.loads(arr)\n",
  81. " print(arr_list[0])\n"
  82. ]
  83. }
  84. ],
  85. "metadata": {
  86. "kernelspec": {
  87. "display_name": "Python 3",
  88. "language": "python",
  89. "name": "python3"
  90. },
  91. "language_info": {
  92. "codemirror_mode": {
  93. "name": "ipython",
  94. "version": 3
  95. },
  96. "file_extension": ".py",
  97. "mimetype": "text/x-python",
  98. "name": "python",
  99. "nbconvert_exporter": "python",
  100. "pygments_lexer": "ipython3",
  101. "version": "3.6.1"
  102. }
  103. },
  104. "nbformat": 4,
  105. "nbformat_minor": 2
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement