How can I extract a JSON object from a CSV file in Python?

You_got_it

I am extracting column values from a csv file (not the whole file). The values are in JSON format as follows:

{u'Other': {'Text': 'Telephone', 'start': 45, 'end': 54, u'value': u'contact information'}}

I am able to get these values into a list with the following code (json objects = [6] indicates the seventh column in my csv file):

import csv

with open('C:\\file\\path\\to\\csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')

    json_objects = [6]

    for row in reader:

        single_json = list(row[i] for i in json_objects)
        print ', '.join(single_json)

How can I extract the columns as JSON's; not as lists?

David Cullen

This should do what you want:

#!/usr/bin/python

import csv
import json

with open('csv', 'r') as csvfile:
    reader = csv.reader(csvfile, delimiter=',', quotechar='"')
    for row in reader:
        single_json = row[6]
        single_json = single_json.replace("u'", "'")
        single_json = single_json.replace("'", '"')
        data = json.loads(single_json)
        print json.dumps(data, indent=4)

Input file named "csv":

2840,test_category_labeling_highlight,84,3635,0,Other,"{u'Other': {'Text': 'Telephone', 'start': 45, 'end': 54, u'value': u'contact information'}}",8,7,FALSE

Output:

{
    "Other": {
        "Text": "Telephone",
        "end": 54,
        "value": "contact information",
        "start": 45
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I extract the folder path from file path in Python?

How can I extract the folder path from file path in Python?

How can I extract the raw JSON string from an OkHttp Response object?

How do I extract rows from a PDF file into a csv file?

How I can extract object from JSON result usinig angularJS

How can I pickle a python object into a csv file?

How can I extract this data from JSON from API in Python?

Using JMeter, how can I extract a string from the body of a response of an API and save it to a csv file?

How to extract a a specific object from JSON file using python json

How can I extract an object from array?

How can I create a json file from object with serializer?

How can I use Python to generate nested JSON data from my CSV file

How can I extract a text from a bytes file using python

How can I extract data from JSON?

How to extract data from .csv file with a given format in .json file in python?

How can I extract an inner object from a potentially invalid JSON string?

How to extract data from json file in python

How can I read a csv.gz file with pyarrow from a file object?

How can I convert a CSV file to a JSON file with a nested json object using Python?

how can i Extract JSON from HTTP Response (python)

How can I make JSON file from a CSV?

How do I extract an object from a json file based on a nested value (using JavaScript)

How can I filter keywords from a csv file using Python

How can I extract this datafram to csv file?

How can I Extract JSON Object that Holds Base64 and File Extension from application/json body in ASP.NET Core

How can I extract data from "List <Contact>" of object and paste it into file.txt in C#?

How can I write filtered results from a JSON file to a CSV file in Python?

How i can calulate the sum from a csv file in python?

How can i read csv from zip file python?

TOP Ranking

HotTag

Archive