首页 > 解决方案 > 为什么我在这里收到语法错误?Python 和 MongoDB

问题描述

这是错误

  File "crime.py", line 106
result_cursor = collection.find({$and: [{'Year': {$gte : 2000}}, {year: {$lte : 2010}}]}, {"Murder": 1, "Robbery": 1, "Burglary": 1, "Theft": 1, "Year": 1})
                                 ^

SyntaxError: invalid syntax

我在这里遇到语法错误,我无法弄清楚。我查看了 find() 文档,没有任何例子可以说明我的情况。这是我的其余代码。我正在从 mongo db 中的大型 CSV 文件中获取数据,并使用 pygal 将其输出到图表中。在第四个问题/图表尝试中,我试图以点图表示形式制作 2000 年至 2010 年谋杀、抢劫、入室盗窃和盗窃统计数据的图表。


#import pymongo module
import pymongo
#import pygal graphing module
import pygal

#Connect to the mongodb server
#change username and password below with your own
myclient = pymongo.MongoClient(host = "mongodb://localhost:27017/",
serverSelectionTimeoutMS = 3000,
username="",
password='')

#Select the module12 database
db = myclient["mod12"]

#Choose the crime collection from the module12 database
collection = db["crime"]

#Plot the annual statistics for Murder and Robbery for the entire dataset  (1994 - 2013)
result_cursor = collection.find({},{"Murder": 1, "Robbery": 1, "Year": 1})

#Create empty lists to store murder and robbery data in
murder = []
robbery = []
years = []

#store the murder and robbery stats into lists
for doc in result_cursor:
    murder.append(doc["Murder"])
    robbery.append(doc["Robbery"])
    years.append(doc["Year"])

#Export the data in a chart to an SVG file
bar_chart = pygal.Bar()
bar_chart.title = "Demo: Murder & Robbery Stats"
bar_chart.x_labels = map(str, years) #set x axis labels to the years
bar_chart.add('Murder', murder) # add murder data to the graph using the murder list
bar_chart.add('Robbery',robbery) # add robbery data to the graph using the robbery list
bar_chart.render_to_file('images/test_chart.svg') #render the chart to an SVG file

#If program is successful print this
print("Program executed successfully")

### Write your queries and create your charts below

result_cursor = collection.find({},{"Burglary" : 1, "Theft": 1, "Year":1})

burglary = [] 
theft = []
years = []

for doc in result_cursor:
    burglary.append(doc["Burglary"])
    theft.append(doc["Theft"])
    years.append(doc["Year"])

bar_chart = pygal.Bar()
bar_chart.title = "Burglary and Theft Stats"
bar_chart.x_labels = map(str, years)
bar_chart.add('Burglary', burglary)
bar_chart.add('Theft', theft)
bar_chart.render_to_file('images/question_1.svg')

print("Q1 done right")

result_cursor = collection.find({},{"Motor Vehicle Theft": 1, "Property Crime": 1, "Year": 1})

motortheft = []
propertycrime = [] 
years = []

for doc in result_cursor:
    motortheft.append(doc["Mother Vehicle Theft"])
    propertycrime.append(doc["Property Crime"])
    years.append(doc["Year"])

line_chart = pygal.Line()
line_chart.title = 'Motor Vehicle Theft and Property Crime stats'
line_chart.x_labels = map(str, years)
line_chart.add('Motor Vehicle Theft', motortheft)
line_chart.add('Property Crime', propertycrime)
line_chart.render_to_file('images/question_2.svg')

print("Q2 done right")

result_cursor = collection.find({},{"Violent Crime": 1, "Aggravated Assault": 1})

vcrime = []
aassault = [] 

for doc in result_cursor:
    vcrime.append(doc["Violent Crime"])
    aassault.append(doc["Aggravated Assault"])

line_chart = pygal.HorizontalBar()
line_chart.title = 'Violent Crime and Aggravated Assualt Stats'
line_chart.add('Violent Crime', vcrime)
line_chart.add('Aggravated Assault', aassault)
line_chart.render_to_file('images/question_3.svg')

print("Q3 done right")


**result_cursor = collection.find({$and: [{'Year': {$gte : 2000}}, {year: {$lte : 2010}}]}, {"Murder": 1, "Robbery": 1, "Burglary": 1, "Theft": 1, "Year": 1})**

murder = []
robbery = []
burglary = []
theft = []
years = []
for doc in result_cursor:
    murder.append(doc["Murder"])
    robbery.append(doc["Robbery"])
    burglary.append(doc["Burglary"])
    theft.append(doc["Theft"])
    years.append(doc["Year"])

dot_chart = pygal.Dot(x_label_rotation=30)
dot_chart.title = 'Murder, Robbery, Burglary, & Theft Stats 2000-2010'
dot_chart.x_labels = map(str, years)
dot_chart.add('Murder', murder)
dot_chart.add('Robbery', robbery)
dot_chart.add('Burglary', burglary)
dot_chart.add('Theft', theft)
dot_chart.render_to_file('images/question_4.svg')

print("Q4 done")


标签: pythonmongodbsvgpygal

解决方案


推荐阅读