0

I have a CSV JSON file of movie casts with 5000 items. The first item looks like this:

[{
    "cast_id": 5,
    "character": "John Carter",
    "credit_id": "52fe479ac3a36847f813ea75",
    "gender": 2,
    "id": 60900,
    "name": "Taylor Kitsch",
    "order": 0
}, {
    "cast_id": 20,
    "character": "Dejah Thoris",
    "credit_id": "52fe479ac3a36847f813eab3",
    "gender": 1,
    "id": 21044,
    "name": "Lynn Collins",
    "order": 1
}, {
    "cast_id": 7,
    "character": "Sola",
    "credit_id": "52fe479ac3a36847f813ea79",
    "gender": 1,
    "id": 2206,
    "name": "Samantha Morton",
    "order": 2
}, {
    "cast_id": 3,
    "character": "Tars Tarkas",
    "credit_id": "52fe479ac3a36847f813ea6d",
    "gender": 2,
    "id": 5293,
    "name": "Willem Dafoe",
    "order": 3
}, {
    "cast_id": 8,
    "character": "Tal Hajus",
    "credit_id": "52fe479ac3a36847f813ea7d",
    "gender": 2,
    "id": 19159,
    "name": "Thomas Haden Church",
    "order": 4
}, {
    "cast_id": 2,
    "character": "Matai Shang",
    "credit_id": "52fe479ac3a36847f813ea69",
    "gender": 2,
    "id": 2983,
    "name": "Mark Strong",
    "order": 5
}, {
    "cast_id": 4,
    "character": "Tardos Mors",
    "credit_id": "52fe479ac3a36847f813ea71",
    "gender": 2,
    "id": 8785,
    "name": "Ciar\u00e1n Hinds",
    "order": 6
}, {
    "cast_id": 9,
    "character": "Sab Than",
    "credit_id": "52fe479ac3a36847f813ea81",
    "gender": 2,
    "id": 17287,
    "name": "Dominic West",
    "order": 7
}, {
    "cast_id": 10,
    "character": "Kantos Kan",
    "credit_id": "52fe479ac3a36847f813ea85",
    "gender": 2,
    "id": 17648,
    "name": "James Purefoy",
    "order": 8
}, {
    "cast_id": 11,
    "character": "Powell",
    "credit_id": "52fe479ac3a36847f813ea89",
    "gender": 2,
    "id": 17419,
    "name": "Bryan Cranston",
    "order": 9
}, {
    "cast_id": 12,
    "character": "Sarkoja",
    "credit_id": "52fe479ac3a36847f813ea8d",
    "gender": 1,
    "id": 6416,
    "name": "Polly Walker",
    "order": 10
}, {
    "cast_id": 13,
    "character": "Edgar Rice Burroughs",
    "credit_id": "52fe479ac3a36847f813ea91",
    "gender": 2,
    "id": 57675,
    "name": "Daryl Sabara",
    "order": 11
}, {
    "cast_id": 14,
    "character": "Stayman #1 / Helm",
    "credit_id": "52fe479ac3a36847f813ea95",
    "gender": 2,
    "id": 89830,
    "name": "Arkie Reece",
    "order": 12
}, {
    "cast_id": 15,
    "character": "Stayman #3",
    "credit_id": "52fe479ac3a36847f813ea99",
    "gender": 2,
    "id": 205278,
    "name": "Davood Ghadami",
    "order": 13
}, {
    "cast_id": 16,
    "character": "Lightmaster",
    "credit_id": "52fe479ac3a36847f813ea9d",
    "gender": 1,
    "id": 218345,
    "name": "Pippa Nixon",
    "order": 14
}, {
    "cast_id": 46,
    "character": "Thern #2",
    "credit_id": "584ef986c3a3682a940010d0",
    "gender": 2,
    "id": 1390394,
    "name": "James Embree",
    "order": 15
}, {
    "cast_id": 77,
    "character": "Thern #1",
    "credit_id": "58c68f82c3a3684114014f58",
    "gender": 0,
    "id": 1518112,
    "name": "Philip Philmar",
    "order": 16
}, {
    "cast_id": 47,
    "character": "Pretty Woman in NYC Doorway",
    "credit_id": "584f133992514107110024b8",
    "gender": 1,
    "id": 1721985,
    "name": "Emily Tierney",
    "order": 17
}, {
    "cast_id": 48,
    "character": "Telegraph Clerk",
    "credit_id": "584f16d192514107000026a2",
    "gender": 2,
    "id": 1721992,
    "name": "Edmund Kente",
    "order": 18
}, {
    "cast_id": 49,
    "character": "Dalton",
    "credit_id": "584f1a94c3a3682a8d0026e7",
    "gender": 2,
    "id": 118617,
    "name": "Nicholas Woodeson",
    "order": 19
}, {
    "cast_id": 50,
    "character": "Stable Boy",
    "credit_id": "584f1f2b9251410700002be9",
    "gender": 2,
    "id": 1722006,
    "name": "Kyle Agnew",
    "order": 20
}, {
    "cast_id": 51,
    "character": "Dix the Storekeeper",
    "credit_id": "584f28aec3a3683150000214",
    "gender": 2,
    "id": 130129,
    "name": "Don Stark",
    "order": 21
}, {
    "cast_id": 52,
    "character": "Rowdy #1",
    "credit_id": "58580465c3a3683150056d0c",
    "gender": 2,
    "id": 65716,
    "name": "Josh Daugherty",
    "order": 22
}, {
    "cast_id": 53,
    "character": "Rowdy #2",
    "credit_id": "58580cd89251411a4605f517",
    "gender": 2,
    "id": 1724736,
    "name": "Jared Cyr",
    "order": 23
}, {
    "cast_id": 37,
    "character": "Stockade Guard",
    "credit_id": "54e5a58d925141529c000f89",
    "gender": 2,
    "id": 62082,
    "name": "Christopher Goodman",
    "order": 24
}, {
    "cast_id": 54,
    "character": "Sarah Carter",
    "credit_id": "585823dc925141594100c816",
    "gender": 1,
    "id": 1367241,
    "name": "Amanda Clayton",
    "order": 25
}, {
    "cast_id": 170,
    "character": "Apache #1 (as Joe Billingiere)",
    "credit_id": "595ad40c9251410bfa04831e",
    "gender": 0,
    "id": 1844319,
    "name": "Joseph Billingiere",
    "order": 26
}
]

I only need the values of "name" in this file. For eg in this item it would be:

Taylor Kitsch, Lynn Collins, Samantha Morton, Willem Dafoe, Thomas Haden Church, Mark Strong, Ciar Hinds, Dominic West, James Purefoy, Bryan Cranston, Polly Walker etc.

Meaning values with key as "name".

How should I go about doing it?

1
  • 3
    That's not CSV, it's JSON Commented Apr 7, 2018 at 1:31

5 Answers 5

3

What you've shown is a single JSON array, not a CSV. (File extensions don't matter to Python)

Parse the names from the objects in each JSON array row

import json

with open("file.txt") as f:
    for line in f:
        names = (x['name'] for x in json.loads(line))
        for name in names:
            print(name) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer! I was able to get all the names but the thing is they are all together one after the other meaning I cannot distinguish them based on the movies. Can I arrange them specific to a particular movie?
It's not clear what part of your data contains any information about a particular movie, but you could start by replacing the inner loop here with just print(list(names))
2

Something like this should work. You use the csv library to convert the rows into an array to iterate over. The ast library will turn a string into a dict/json object you can key into.

import ast
import csv
names = []

with open('csvFile.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        names.append(ast.literal_eval(row)[name])
        # or you could print(name) here. 

1 Comment

Did you try this with a CSV file containing the row provided in the question?
1

This is a very rudimentary example based on what you provided:

>>> movie =  [{"cast_id": 5, "character": "John Carter", "credit_id": "52fe479ac3a36847f813ea75", "gender": 2, "id": 60900, "name": "Taylor Kitsch", "order": 0}, {"cast_id": 20, "character": "Dejah Thoris", "credit_id": "52fe479ac3a36847f813eab3", "gender": 1, "id": 21044, "name": "Lynn Collins", "order": 1}]
>>> for i in movie:
...     print(i["name"])
...
Taylor Kitsch
Lynn Collins

Basically, this iterates over the different dictionaries in the list and then pulls the value associated with the "name" key.

More information is required to do this over an entire csv file, but this might get you started.

4 Comments

I have 5000 rows like this in an excel file. How could I get actors' name for all these 5000 rows?
Have you read the csv file into python yet? I encourage you to check out the csv module for python if you haven't already; more specifically the csv.reader and csv.DictReader classes.
I am just going off what the OP gave. I don't have enough Karma to comment the OP for clarification... But you are right, looking at it now, that is a JSON file, not a CSV.
I have a csv file with multiple columns from which I have extracted single column of casts. This column has 5000 rows in the form of JSON
0

You can directly make request with filter

{'$filter': ['name eq Lynn Collins']}

Comments

-2
my_list = [{
    "cast_id": 5,
    "character": "John Carter",
    "credit_id": "52fe479ac3a36847f813ea75",
    "gender": 2,
    "id": 60900,
    "name": "Taylor Kitsch",
    "order": 0
}, {
    "cast_id": 20,
    "character": "Dejah Thoris",
    "credit_id": "52fe479ac3a36847f813eab3",
    "gender": 1,
    "id": 21044,
    "name": "Lynn Collins",
    "order": 1
}, {
    "cast_id": 7,
    "character": "Sola",
    "credit_id": "52fe479ac3a36847f813ea79",
    "gender": 1,
    "id": 2206,
    "name": "Samantha Morton",
    "order": 2
}, {
    "cast_id": 3,
    "character": "Tars Tarkas",
    "credit_id": "52fe479ac3a36847f813ea6d",
    "gender": 2,
    "id": 5293,
    "name": "Willem Dafoe",
    "order": 3
}, {
    "cast_id": 8,
    "character": "Tal Hajus",
    "credit_id": "52fe479ac3a36847f813ea7d",
    "gender": 2,
    "id": 19159,
    "name": "Thomas Haden Church",
    "order": 4
}, {
    "cast_id": 2,
    "character": "Matai Shang",
    "credit_id": "52fe479ac3a36847f813ea69",
    "gender": 2,
    "id": 2983,
    "name": "Mark Strong",
    "order": 5
}, {
    "cast_id": 4,
    "character": "Tardos Mors",
    "credit_id": "52fe479ac3a36847f813ea71",
    "gender": 2,
    "id": 8785,
    "name": "Ciar\u00e1n Hinds",
    "order": 6
}, {
    "cast_id": 9,
    "character": "Sab Than",
    "credit_id": "52fe479ac3a36847f813ea81",
    "gender": 2,
    "id": 17287,
    "name": "Dominic West",
    "order": 7
}, {
    "cast_id": 10,
    "character": "Kantos Kan",
    "credit_id": "52fe479ac3a36847f813ea85",
    "gender": 2,
    "id": 17648,
    "name": "James Purefoy",
    "order": 8
}, {
    "cast_id": 11,
    "character": "Powell",
    "credit_id": "52fe479ac3a36847f813ea89",
    "gender": 2,
    "id": 17419,
    "name": "Bryan Cranston",
    "order": 9
}, {
    "cast_id": 12,
    "character": "Sarkoja",
    "credit_id": "52fe479ac3a36847f813ea8d",
    "gender": 1,
    "id": 6416,
    "name": "Polly Walker",
    "order": 10
}, {
    "cast_id": 13,
    "character": "Edgar Rice Burroughs",
    "credit_id": "52fe479ac3a36847f813ea91",
    "gender": 2,
    "id": 57675,
    "name": "Daryl Sabara",
    "order": 11
}, {
    "cast_id": 14,
    "character": "Stayman #1 / Helm",
    "credit_id": "52fe479ac3a36847f813ea95",
    "gender": 2,
    "id": 89830,
    "name": "Arkie Reece",
    "order": 12
}, {
    "cast_id": 15,
    "character": "Stayman #3",
    "credit_id": "52fe479ac3a36847f813ea99",
    "gender": 2,
    "id": 205278,
    "name": "Davood Ghadami",
    "order": 13
}, {
    "cast_id": 16,
    "character": "Lightmaster",
    "credit_id": "52fe479ac3a36847f813ea9d",
    "gender": 1,
    "id": 218345,
    "name": "Pippa Nixon",
    "order": 14
}, {
    "cast_id": 46,
    "character": "Thern #2",
    "credit_id": "584ef986c3a3682a940010d0",
    "gender": 2,
    "id": 1390394,
    "name": "James Embree",
    "order": 15
}, {
    "cast_id": 77,
    "character": "Thern #1",
    "credit_id": "58c68f82c3a3684114014f58",
    "gender": 0,
    "id": 1518112,
    "name": "Philip Philmar",
    "order": 16
}, {
    "cast_id": 47,
    "character": "Pretty Woman in NYC Doorway",
    "credit_id": "584f133992514107110024b8",
    "gender": 1,
    "id": 1721985,
    "name": "Emily Tierney",
    "order": 17
}, {
    "cast_id": 48,
    "character": "Telegraph Clerk",
    "credit_id": "584f16d192514107000026a2",
    "gender": 2,
    "id": 1721992,
    "name": "Edmund Kente",
    "order": 18
}, {
    "cast_id": 49,
    "character": "Dalton",
    "credit_id": "584f1a94c3a3682a8d0026e7",
    "gender": 2,
    "id": 118617,
    "name": "Nicholas Woodeson",
    "order": 19
}, {
    "cast_id": 50,
    "character": "Stable Boy",
    "credit_id": "584f1f2b9251410700002be9",
    "gender": 2,
    "id": 1722006,
    "name": "Kyle Agnew",
    "order": 20
}, {
    "cast_id": 51,
    "character": "Dix the Storekeeper",
    "credit_id": "584f28aec3a3683150000214",
    "gender": 2,
    "id": 130129,
    "name": "Don Stark",
    "order": 21
}, {
    "cast_id": 52,
    "character": "Rowdy #1",
    "credit_id": "58580465c3a3683150056d0c",
    "gender": 2,
    "id": 65716,
    "name": "Josh Daugherty",
    "order": 22
}, {
    "cast_id": 53,
    "character": "Rowdy #2",
    "credit_id": "58580cd89251411a4605f517",
    "gender": 2,
    "id": 1724736,
    "name": "Jared Cyr",
    "order": 23
}, {
    "cast_id": 37,
    "character": "Stockade Guard",
    "credit_id": "54e5a58d925141529c000f89",
    "gender": 2,
    "id": 62082,
    "name": "Christopher Goodman",
    "order": 24
}, {
    "cast_id": 54,
    "character": "Sarah Carter",
    "credit_id": "585823dc925141594100c816",
    "gender": 1,
    "id": 1367241,
    "name": "Amanda Clayton",
    "order": 25
}, {
    "cast_id": 170,
    "character": "Apache #1 (as Joe Billingiere)",
    "credit_id": "595ad40c9251410bfa04831e",
    "gender": 0,
    "id": 1844319,
    "name": "Joseph Billingiere",
    "order": 26
}
]

my_names = [temp_dict['name'] for temp_dict in my_list]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.