python - 为什么我在这里收到语法错误?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")
解决方案
推荐阅读
- react-native - React Native 和 Kotlin 哪个更适合跨平台开发?
- azure-logic-apps - 逻辑应用响应“ActionResponseTimedOut”问题
- django - 使用 django rest 框架的管理页面的行级权限
- excel - 为什么这个检查百分比总数的公式有时会失败?(总和(B4:B7)-1)
- multithreading - 当 esp/ebp 移动时,调用堆栈上的堆栈帧的内存如何被释放?
- c++ - c++中的舍入函数
- model-view-controller - 为什么不能将数据绑定到 Telerik kendo 下拉列表
- ionic3 - Ionic 3 - Ms Adal :: 如何缓存数据并重新发送
- java - 如何在 OpenJDK 中创建一个新堆?
- c++ - Visual Studio 未在所有文件中找到或替换