当前位置: 首页 > news >正文

做网站的成功案例/厦门网站的关键词自动排名

做网站的成功案例,厦门网站的关键词自动排名,个人企业信息查询,wordpress 访问不了系列文章总目录:Python数据分析及可视化实例目录1.MongoDB安装有时候度娘还是给力的,相反一些博客的安装方法则显得凌乱:http://jingyan.baidu.com/article/d5c4b52bef7268da560dc5f8.html最初使用Win7安装经常会出现意料不到的错误&#xff…

0c203cd825851b771de2cb7e82d81f97.png

系列文章总目录:Python数据分析及可视化实例目录


386e19af714dc59c25d42fb0e579ecde.png

1.MongoDB安装

有时候度娘还是给力的,相反一些博客的安装方法则显得凌乱:

http://jingyan.baidu.com/article/d5c4b52bef7268da560dc5f8.html

最初使用Win7安装经常会出现意料不到的错误,

比如开机启动服务,服务器日志等。

上面链接安装步骤简单,没有炫技的成分,赞一个。

2.基本操作:增删改查

在Python中操作该数据库,需要安装依赖库Pymongo,

没有什么说明比官方文档更好的了:PyMongo 3.5.1 Documentation

如果你采用其他的库请参照响应的文档,比如MongoEngine。

下面的代码在Pymongo文档的基础上DIY类,比较完善。

不用该类也是可以的,用的时候再直接粗暴操作,MongoDB也喜欢。

# encoding: utf-8
__author__ = 'yeayee'  2015-07from pymongo import MongoClient
from pymongo import errors
import json
from datetime import date, datetime# 数据库日志配置
from log import Logger
my_logger = Logger('mongodb', 'mongodb.log', 'DEBUG')
my_logger.set_file_level('DEBUG')
my_logger.set_stream_level('WARNING')  # WARNING DEBUG
my_logger.set_stream_handler_fmt('%(message)s')
my_logger.load()
logger = my_logger.logger
# my_logger.get_memory_usage()class Mongodb():"""自定义mongodb工具"""def __init__(self, db_config, db_name=None):self.db_config = db_configif db_name is not None:self.db_config['database'] = db_nametry:# 实例化mongodbself.conn = MongoClient(self.db_config['host'], self.db_config['port'])# 获取数据库对象(选择/切换)self.db = self.conn.get_database(self.db_config['database'])except errors.ServerSelectionTimeoutError, e:logger.error('连接超时:%s' % e)except Exception as e:logger.error(e)@staticmethoddef __default(obj):"""支持datetime的json encodeTypeError: datetime.datetime(2015, 10, 21, 8, 42, 54) is not JSON serializable:param obj::return:"""if isinstance(obj, datetime):return obj.strftime('%Y-%m-%d %H:%M:%S')elif isinstance(obj, date):return obj.strftime('%Y-%m-%d')else:raise TypeError('%r is not JSON serializable' % obj)def close_conn(self):"""关闭连接关闭所有套接字的连接池和停止监控线程。如果这个实例再次使用它将自动重启和重新启动线程"""self.conn.close()def find_one(self, table_name, condition=None):"""查询单条记录:param table_name::param condition::return:"""return self.db.get_collection(table_name).find_one(condition)def find_all(self, table_name, condition=None):"""查询多条记录:param table_name::param condition::return:"""return self.db.get_collection(table_name).find(condition)def count(self, table_name, condition=None):"""查询记录总数:param table_name::param condition::return:"""return self.db.get_collection(table_name).count(condition)def distinct(self, table_name, field_name):"""查询某字段去重后值的范围:param table_name::param field_name::return:"""return self.db.get_collection(table_name).distinct(field_name)def insert(self, table_name, data):"""插入数据:param table_name::param data::return:"""try:ids = self.db.get_collection(table_name).insert(data)return idsexcept Exception, e:logger.error('插入失败:%s' % e)return Nonedef update(self, table_name, condition, update_data, update_type='set'):"""批量更新数据upsert : 如果不存在update的记录,是否插入;true为插入,默认是false,不插入。:param table_name::param condition::param update_data::param update_type: 范围:['inc', 'set', 'unset', 'push', 'pushAll', 'addToSet', 'pop', 'pull', 'pullAll', 'rename']:return:"""if update_type not in ['inc', 'set', 'unset', 'push', 'pushAll', 'addToSet', 'pop', 'pull', 'pullAll', 'rename']:logger.error('更新失败,类型错误:%s' % update_type)return Nonetry:result = self.db.get_collection(table_name).update_many(condition, {'$%s' % update_type: update_data})logger.info('更新成功,匹配数量:%s;更新数量:%s' % (result.matched_count, result.modified_count))return result.modified_count  # 返回更新数量,仅支持MongoDB 2.6及以上版本except Exception, e:logger.error('更新失败:%s' % e)return Nonedef remove(self, table_name, condition=None):"""删除文档记录:param table_name::param condition::return:"""result = self.db.get_collection(table_name).remove(condition)if result.get('err') is None:logger.info('删除成功,删除行数%s' % result.get('n', 0))return result.get('n', 0)else:logger.error('删除失败:%s' % result.get('err'))return Nonedef output_row(self, table_name, condition=None, style=0):"""格式化输出单个记录style=0 键值对齐风格style=1 JSON缩进风格:param table_name::param condition::param style::return:"""row = self.find_one(table_name, condition)if style == 0:# 获取KEY最大的长度作为缩进依据max_len_key = max([len(each_key) for each_key in row.keys()])str_format = '{0: >%s}' % max_len_keykeys = [str_format.format(each_key) for each_key in row.keys()]result = dict(zip(keys, row.values()))print ('**********  表名[%s]  **********' % table_name)for key, item in result.items():print key, ':', itemelse:print (json.dumps(row, indent=4, ensure_ascii=False, default=self.__default))def output_rows(self, table_name, condition=None, style=0):"""格式化输出批量记录style=0 键值对齐风格style=1 JSON缩进风格:param table_name::param condition::param style::return:"""rows = self.find_all(table_name, condition)total = self.count(table_name, condition)if style == 0:count = 0for row in rows:# 获取KEY最大的长度作为缩进依据max_len_key = max([len(each_key) for each_key in row.keys()])str_format = '{0: >%s}' % max_len_keykeys = [str_format.format(each_key) for each_key in row.keys()]result = dict(zip(keys, row.values()))count += 1print ('**********  表名[%s]  [%d/%d]  **********' % (table_name, count, total))for key, item in result.items():print key, ':', itemelse:for row in rows:print (json.dumps(row, indent=4, ensure_ascii=False, default=self.__default))

注:上面的类初学者能看懂最好,看不懂就只看数据库操作相关命令即可。 之所以分享这个类操作,在Flask Web开发过程中要用到类的继承,实现记录用户登录状态。如果你接下来并不打算开发Web也不想用Web数据可视化,那么给你最粗暴的操作方法:

http://api.mongodb.com/python/current/tutorial.html

# encoding: utf-8
__author__ = 'yeayee'  2015-08
from pymongo import MongoClient
client = MongoClient(host='localhost', port=27017)
db = client.test # 直接建立一个测试数据库
collection = db.students # 在上数据库建立一个集合
student = {'id': '20170101','name': 'Jordan','age': 20,'gender': 'male'
}  # 声明一条数据# 增
collection.insert(student)  # 插入数据
# 改,发现年龄错了,加1岁
collection.update_one({'name': 'Jordan'}, {'$inc': {'age': 1}})
# 查
result = collection.find_one({'name': 'Jordan'})  # 查询数据,返回的是一个游标
print(result)
# <pymongo.results.UpdateResult object at 0x10c6384c8>
# 删
result = collection.remove({'name': 'Jordan'})  # 返回删除状态
print(result)
# {'ok': 1, 'n': 1}

注:增删改查没这么简单,只是举了一个最简单的例子,有一本《MongoDB权威指南》,可以自己找PDF版;如果决定用这货,黑框下面操作MongoDB是逃不掉的。此外,在win平台下推荐软件Robomongo ,可视化操作数据集合。嗯,具体到插入多条,更改多条数据,聚类操作、地理位置信息聚类等,等实例源码中慢慢解说。

胶水语言博大精深,

本主只得一二为新人带路,

新手可查阅历史目录:

yeayee:Python数据分析及可视化实例目录​zhuanlan.zhihu.com
2e7947e18503ed8a0ff72714b8f60940.png

最后,别只收藏不关注哈

http://www.jmfq.cn/news/5244805.html

相关文章:

  • 做电商网站的步骤/放心网站推广优化咨询
  • 公共网站怎地做/百度开户流程
  • discuz建网站/今天的国际新闻
  • 网站建设 试卷/海外推广解决方案
  • 免费博客网站有哪些/好看的html网页
  • 给别人云做网站赚钱吗/必应搜索国际版
  • 英语网站排名/天津百度推广开户
  • 企业网站优化应该怎么做/买链接网
  • 大同哪有做网站的/在线网络培训平台
  • php做网站用什么软件好/楚雄今日头条新闻
  • 有免费的网站做农家院宣传/seo排名点击首页
  • 网站建设 团队/站长素材官网免费
  • 做最好的色书网站/李勇seo的博客
  • 如果在浏览器上做一网站广告大约需要多少钱/seo设置是什么
  • 云网站后台/做做网站
  • 可以做t恤的网站/seo网络营销外包公司
  • 软件开发培训机构培训出来的/seo网络推广经理
  • 做网站全屏图片拉长代码/高端网站建设专业公司
  • 苏州建设局网站/公司网站推广
  • 高端网站建设哪家更专业/深圳seo优化公司
  • 网站运营与管理的内容有哪些/网页优化建议
  • 网站备案无前置审批文件/百度在西安有分公司吗
  • 精仿源码社区网站源码/怎么做网页设计的页面
  • 网站购物功能如何做/百度趋势搜索大数据
  • 网站打开是目录结构图/唯尚广告联盟平台
  • 中山网站建设托管/站长工具是什么
  • 关于我们网页设计模板/石家庄网站建设seo
  • 动态网站完整版/网络推广公司收费标准
  • 网站绿色图片什么颜色做底色/seo网站关键词优化报价
  • 网站联系方式连接怎么做/app拉新