0

i'm new to Python. I want to insert specific columns of a csv file into a collection in my mongodb, i know how to do it with the shell but i want to do it with python, as i'm learning Python.

This is what i have so far:

import csv
import json
import pandas as pd
import sys, getopt, pprint
from pymongo import MongoClient
#CSV to JSON Conversion
with open('netflix_titles.csv','rt', encoding="utf8") as file:
data = file.read()
mongo_client=MongoClient() 
db=mongo_client.Netflix
db.segment.drop()
header= [ "show_id", "director"]

for each in data:
    row={}
    for field in header:
        row[field]=each[field]  

    db.segment.insert(row)

In only throws me an indented error in this line:

data = file.read()
0

2 Answers 2

1

If you are going to use the csv module then definitely use the reader function of that module. It will deal with quotes, whitespace, etc. etc. Also, not sure if your

for each in data:

Is actually doing the CSV breakup you think it is. This is more tidy (assuming you want to drive the action from a static array of header names):

with open('data.csv', 'r') as csvfile:
    header = [ "show_id", "director"]
    reader = csv.reader(csvfile)

    for row in reader:
        doc={}
        for n in range(0,len(header)):
            doc[header[n]] = row[n]

        db.foo.insert(doc)
Sign up to request clarification or add additional context in comments.

Comments

1

You have to put tab before date = file.read() , because with increase indentation level.

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.