首先我将csv文件转换为json文件。我试图读取该文件,它仅为标题&抛出对象id。没有为剩余数据生成id ..
代码:
import csv
import json
csvfile = open('C://test//08October2014//DATA_INTV_NEW//Oct08//final-current.csv', 'r')
jsonfile = open('C://test//08October2014//DATA_INTV_NEW//Oct08//final-current.json', 'w')
reader = csv.DictReader( csvfile )
for row in reader:
json.dump(row, jsonfile)
jsonfile.write('\n')
print row
输出:
{'High Price': '0', 'S No': '2', 'Buy Price': '217.85', 'Instrument Name': 'DABUR14NOVFUT', 'Last Traded Price': '219.85', 'Average Traded Price': '0', 'Time': '9:25:00', 'Sell Quantity': '4000', 'Sell Price': '220.3', 'Low Price': '0', 'Close Price': '218.7', 'V': '0', 'Total Traded Quantity': '0', 'Buy Quantity': '6000', 'Open Price': '0'}
{'High Price': '0', 'S No': '18', 'Buy Price': '447.05', 'Instrument Name': 'RELCAPITAL14DECFUT', 'Last Traded Price': '485.15', 'Average Traded Price': '0', 'Time': '9:25:00', 'Sell Quantity': '2000', 'Sell Price': '455', 'Low Price': '0', 'Close Price': '485.15', 'V': '0', 'Total Traded Quantity': '0', 'Buy Quantity': '1000', 'Open Price': '0'}
{'High Price': '54.5', 'S No': '25', 'Buy Price': '54.1', 'Instrument Name': 'DISHTV14NOVFUT', 'Last Traded Price': '54.5', 'Average Traded Price': '54.5', 'Time': '9:25:00', 'Sell Quantity': '16000', 'Sell Price': '54.85', 'Low Price': '54.5', 'Close Price': '54.35', 'V': '8000', 'Total Traded Quantity': '8000', 'Buy Quantity': '16000', 'Open Price': '54.5'}
{'High Price': '167', 'S No': '26', 'Buy Price': '166.7', 'Instrument Name': 'IDEA14OCTFUT', 'Last Traded Price': '166.6', 'Average Traded Price': '166.23', 'Time': '9:25:00', 'Sell Quantity': '2000', 'Sell Price': '167.15', 'Low Price': '165.6', 'Close Price': '166.75', 'V': '168000', 'Total Traded Quantity': '206000', 'Buy Quantity': '2000', 'Open Price': '165.65'}
{'High Price': '0', 'S No': '45', 'Buy Price': '948', 'Instrument Name': 'RELIANCE14DECFUT', 'Last Traded Price': '947.45', 'Average Traded Price': '0', 'Time': '9:25:00', 'Sell Quantity': '250', 'Sell Price': '951.2', 'Low Price': '0', 'Close Price': '947.35', 'V': '0', 'Total Traded Quantity': '0', 'Buy Quantity': '250', 'Open Price': '0'}
{'High Price': '0', 'S No': '66', 'Buy Price': '564.35', 'Instrument Name': 'RELINFRA14DECFUT', 'Last Traded Price': '0', 'Average Traded Price': '0', 'Time': '9:25:00', 'Sell Quantity': '2000', 'Sell Price': '594.1', 'Low Price': '0', 'Close Price': '611.7', 'V': '0', 'Total Traded Quantity': '0', 'Buy Quantity': '2000', 'Open Price': '0'}
{'High Price': '135.7', 'S No': '74', 'Buy Price': '135', 'Instrument Name': 'IDFC14OCTFUT', 'Last Traded Price': '134.95', 'Average Traded Price': '135.21', 'Time': '9:25:00', 'Sell Quantity': '8000', 'Sell Price': '135.1', 'Low Price': '134.85', 'Close Price': '135.7', 'V': '532000', 'Total Traded Quantity': '1196000', 'Buy Quantity': '2000', 'Open Price': '135.65'}
{'High Price': '0', 'S No': '92', 'Buy Price': '71.15', 'Instrument Name': 'RPOWER14DECFUT', 'Last Traded Price': '71', 'Average Traded Price': '0', 'Time': '9:25:00', 'Sell Quantity': '4000', 'Sell Price': '71.6', 'Low Price': '0', 'Close Price': '71', 'V': '0', 'Total Traded Quantity': '0', 'Buy Quantity': '4000', 'Open Price': '0'}
{'High Price': '0', 'S No': '113', 'Buy Price': '69.1', 'Instrument Name': 'SAIL14DECFUT', 'Last Traded Price': '69.85', 'Average Traded Price': '0', 'Time': '9:25:00', 'Sell Quantity': '12000', 'Sell Price': '70.4', 'Low Price': '0', 'Close Price': '69.85', 'V': '0', 'Total Traded Quantity': '0', 'Buy Quantity': '12000', 'Open Price': '0'}
{'High Price': '34.35', 'S No': '120', 'Buy Price': '34.3', 'Instrument Name': 'IFCI14OCTFUT', 'Last Traded Price': '34.3', 'Average Traded Price': '34.15', 'Time': '9:25:00', 'Sell Quantity': '18000', 'Sell Price': '34.35', 'Low Price': '33.8', 'Close Price': '34.1', 'V': '693000', 'Total Traded Quantity': '1197000', 'Buy Quantity': '63000', 'Open Price': '33.8'}
{'High Price': '2419', 'S No': '134', 'Buy Price': '2412.6', 'Instrument Name': 'SBIN14DECFUT', 'Last Traded Price': '2419', 'Average Traded Price': '2419', 'Time': '9:25:00', 'Sell Quantity': '125', 'Sell Price': '2430.1', 'Low Price': '2419', 'Close Price': '2414.45', 'V': '125', 'Total Traded Quantity': '125', 'Buy Quantity': '375', 'Open Price': '2419'}
我的主要流程包含以下代码:
import pandas as pd
import sys, getopt, csv, pprint
from pymongo import MongoClient
mongo_client=MongoClient()
db=mongo_client.october_mug_talk
db.segment.drop()
data=pd.read_csv('C://test//08October2014//DATA_INTV_NEW//Oct08//final-current.json', error_bad_lines=0)
df = pd.DataFrame(data)
records = csv.DictReader(df)
for rown in records:
db.segment.insert(rown)
print rown
但仅为标题生成输出。我不知道我在做错误的地方。请纠正我。
输出:
{'{"High Price": "0"': ' "S No": "2"', '_id': ObjectId('546b0ed5fb2a0301c430733b')}
{'{"High Price": "0"': ' "Buy Price": "217.85"', '_id': ObjectId('546b0ed5fb2a0301c430733c')}
{'{"High Price": "0"': ' "Instrument Name": "DABUR14NOVFUT"', '_id': ObjectId('546b0ed5fb2a0301c430733d')}
{'{"High Price": "0"': ' "Last Traded Price": "219.85"', '_id': ObjectId('546b0ed5fb2a0301c430733e')}
{'{"High Price": "0"': ' "Average Traded Price": "0"', '_id': ObjectId('546b0ed5fb2a0301c430733f')}
{'{"High Price": "0"': ' "Time": "9:25:00"', '_id': ObjectId('546b0ed5fb2a0301c4307340')}
{'{"High Price": "0"': ' "Sell Quantity": "4000"', '_id': ObjectId('546b0ed5fb2a0301c4307341')}
{'{"High Price": "0"': ' "Sell Price": "220.3"', '_id': ObjectId('546b0ed5fb2a0301c4307342')}
{'{"High Price": "0"': ' "Low Price": "0"', '_id': ObjectId('546b0ed5fb2a0301c4307343')}
{'{"High Price": "0"': ' "Close Price": "218.7"', '_id': ObjectId('546b0ed5fb2a0301c4307344')}
{'{"High Price": "0"': ' "V": "0"', '_id': ObjectId('546b0ed5fb2a0301c4307345')}
{'{"High Price": "0"': ' "Total Traded Quantity": "0"', '_id': ObjectId('546b0ed5fb2a0301c4307346')}
{'{"High Price": "0"': ' "Buy Quantity": "6000"', '_id': ObjectId('546b0ed5fb2a0301c4307347')}
{'{"High Price": "0"': ' "Open Price": "0"}', '_id': ObjectId('546b0ed5fb2a0301c4307348')}