How to convert specific CSV format to JSON using Python

user2563044

I have downloaded a CSV file from Google Trends which presents data in this format:

Top cities for golden globes
City,golden globes
New York (United States),100
Los Angeles (United States),91
Toronto (Canada),69

Top regions for golden globes
Region,golden globes
United States,100
Canada,91
Ireland,72
Australia,72

There are 3-4 of these groups separated by whitespace. The first line of each group contains text I want to use as a key, followed by a list of dictionaries I need associated with that key. Does anyone have any advice on some Python tools I could use to make this happen? I'm not having much luck with Python's CSV library.

My desired output from the above CSV would look like this:

{
"Top cities for golden globes" :
   {
      "New York (United States)" : 100,
      "Los Angeles (United States)" : 91,
      "Toronto (Canada)" : 69
   },
"Top regions for golden globes" :
   {
      "United States" : 100,
      "Canada" : 91,
      "Ireland" : 72,
      "Australia" : 72
   }
}
L3viathan

Your input format is so expectable that I would do it by hand, without a CSV library.

import json
from collections import defaultdict

fh = open("yourfile.csv")
result = defaultdict(dict) #dictionary holding the data
current_key = "" #current category
ignore_next = False #flag to skip header

for line in fh:
    line = line.strip() #throw away newline
    if line == "": #line is empty
        current_key = ""
        continue
    if current_key == "": #current_key is empty
        current_key = line #so the current line is the header for the following data
        ignore_next = True
        continue
    if ignore_next: #we're in a line that can be ignored
        ignore_next = False
        continue
    (a,b) = line.split(",")
    result[current_key][a] = b
fh.close()

#pretty-print data
print json.dumps(result, sort_keys=True, indent=4)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to convert nested JSON data to CSV using python?

分類Dev

How to convert txt file to json format with python?

分類Dev

Json to csv. How to convert it?

分類Dev

How to merge all csv files in a specific folder using python and os

分類Dev

How To Convert CSV File to OpenTSDB Format

分類Dev

How to Convert This RGB format in Python

分類Dev

Convert JSON array into CSV using jq

分類Dev

How to convert hadoop sequence file to json format?

分類Dev

How to convert a JSON file into a different format

分類Dev

python3 convert files (from folder and sub folder) to json file on specific format(item include folder name and file name)

分類Dev

How to convert specific string to split "=" for the key and value and convert it into json in golang

分類Dev

Convert json to csv using jq, with array nested within array

分類Dev

How to format a JSON data using PHP

分類Dev

How to get the Json data from eBay api to save in CSV format?

分類Dev

Convert csv to Json to send to watson-personality insight API with Python

分類Dev

How to generate proper csv file format using javascript

分類Dev

How to convert json data from single key to csv

分類Dev

How to convert JSON string into integer in python?

分類Dev

how do i convert format of current time in python

分類Dev

How to convert a txt file with dictionary format to dataframe in python?

分類Dev

how to convert python time.time() to date format for sql insertion

分類Dev

How to run an ANOVA on specific variables in R using a CSV?

分類Dev

How to convert JSON to BSON using Json.NET

分類Dev

How to convert powershell array to json using ConvertTo-Json?

分類Dev

How to clean columns & convert to datetime using python

分類Dev

convert csv to json (nested objects)

分類Dev

How do I convert a csv file with one column to a dictionary in Python?

分類Dev

How to convert date into given format?

分類Dev

How to convert animation to video format?

Related 関連記事

  1. 1

    How to convert nested JSON data to CSV using python?

  2. 2

    How to convert txt file to json format with python?

  3. 3

    Json to csv. How to convert it?

  4. 4

    How to merge all csv files in a specific folder using python and os

  5. 5

    How To Convert CSV File to OpenTSDB Format

  6. 6

    How to Convert This RGB format in Python

  7. 7

    Convert JSON array into CSV using jq

  8. 8

    How to convert hadoop sequence file to json format?

  9. 9

    How to convert a JSON file into a different format

  10. 10

    python3 convert files (from folder and sub folder) to json file on specific format(item include folder name and file name)

  11. 11

    How to convert specific string to split "=" for the key and value and convert it into json in golang

  12. 12

    Convert json to csv using jq, with array nested within array

  13. 13

    How to format a JSON data using PHP

  14. 14

    How to get the Json data from eBay api to save in CSV format?

  15. 15

    Convert csv to Json to send to watson-personality insight API with Python

  16. 16

    How to generate proper csv file format using javascript

  17. 17

    How to convert json data from single key to csv

  18. 18

    How to convert JSON string into integer in python?

  19. 19

    how do i convert format of current time in python

  20. 20

    How to convert a txt file with dictionary format to dataframe in python?

  21. 21

    how to convert python time.time() to date format for sql insertion

  22. 22

    How to run an ANOVA on specific variables in R using a CSV?

  23. 23

    How to convert JSON to BSON using Json.NET

  24. 24

    How to convert powershell array to json using ConvertTo-Json?

  25. 25

    How to clean columns & convert to datetime using python

  26. 26

    convert csv to json (nested objects)

  27. 27

    How do I convert a csv file with one column to a dictionary in Python?

  28. 28

    How to convert date into given format?

  29. 29

    How to convert animation to video format?

ホットタグ

アーカイブ