python学习笔记(一)

1、IndentationError: unindent does not match any outer indentation level

Python脚本运行出现语法错误:
IndentationError: unindent does not match any outer indentation level

一般出现这样问题的原因是:没有对齐,中间穿插有空格和Tab键。
所以Python对格式要求是很严格的。

2、删除Python模块

导入redis模块
已经安装过Python redis模块了,但在导入redis模块,运行时出现如下的问题

>>> runfile('D:/python/connectRedisTest.py', wdir='D:/python')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\anzhuang\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)
  File "D:\anzhuang\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "D:/python/connectRedisTest.py", line 7, in <module>
    import redis
ImportError: No module named redis

已经安装了,但仍提示没有相应的模块,redis模块是从网上下的redis-2.9.1,但不是最新的redis-2.10.3
使用的安装时python setup.py install
网上提到的删除模块有python setup.py uninstall ,但这种方法,不能删除。

删除Python已安装的模块使用的指令是:
pip install 模块名字
如 pip uninstall redis

3、’type’ object has no attribute ‘getitem

3.1 连接本地redis库

以下是一段连接本地redis数据库的测试代码

import redis

class Database:
    def __init__(self):
        self.host = 'localhost'
        self.port = '6379'
        self.db = '1'
       # self.password = ''

    def write(self, website,city,year,month,day,deal_number):
        try:
            key='_'.join([website,city,str(year),str(month),str(day)])
            val=deal_number
            r=redis.StrictRedis(host=self.host, port=self.port)
            r.set(key, val)
        except Exception, exception:
            print exception

    def read(self, website, city, year, month, day):
        try:
            key='_'.join([website, city, str[year], str[month], str[day]])
            r=redis.StrictRedis(host=self.host, port=self.port)
            value=r.get(key)
            print value
            return value
        except Exception, exception:
            print exception


if __name__=='__main__':
    db=Database()
    db.write('meituan', 'beijing', 2015,7,26,8000)
    db.read('meituan', 'beijing', 2015,7,26)

但运行的结果见下,
‘type’ object has no attribute ‘getitem

原因:read函数key='_'.join([website, city, str[year], str[month], str[day]])与write函数的key='_'.join([website,city,str(year),str(month),str(day)])不一样
所以出现了上述问题,应该保持一致。
3.2 连接服务器redis库
import redis

class Database:
    def __init__(self):
        self.host = '10.10.21.21'
        self.port = 6379
        self.db = 1
        self.password = 123456 

    def write(self, website,city,year,month,day,deal_number):
        try:
            key='_'.join([website,city,str(year),str(month),str(day)])
            val=deal_number
            r=redis.StrictRedis(host=self.host, port=self.port)
            r.set(key, val)
        except Exception, exception:
            print exception

    def read(self, website, city, year, month, day):
        try:
            key='_'.join([website, city, str[year], str[month], str[day]])
            r=redis.StrictRedis(host=self.host, port=self.port)
            value=r.get(key)
            print value
            return value
        except Exception, exception:
            print exception


if __name__=='__main__':
    db=Database()
    db.write('meituan', 'beijing', 2015,7,26,8000)
    db.read('meituan', 'beijing', 2015,7,26)

运行出现如下的错误

>>> runfile('D:/python/connectRedisTest.py', wdir='D:/python')
NOAUTH Authentication required.
NOAUTH Authentication required.

正确的代码时见下:

import redis

class Database:
    def __init__(self):
        self.host = '10.10.21.21'
        self.port = 6379
        self.db = 1
        self.password = 123456 

    def write(self, website,city,year,month,day,deal_number):
        try:
            key='_'.join([website,city,str(year),str(month),str(day)])
            val=deal_number
            r=redis.StrictRedis(host=self.host, port=self.port, db=self.db, password=self.password) ##修改
            r.set(key, val)
        except Exception, exception:
            print exception

    def read(self, website, city, year, month, day):
        try:
            key='_'.join([website, city, str[year], str[month], str[day]])
            r=redis.StrictRedis(host=self.host, port=self.port, db=self.db, password=self.password)  ##修改
            value=r.get(key)
            print value
            return value
        except Exception, exception:
            print exception


if __name__=='__main__':
    db=Database()
    db.write('meituan', 'beijing', 2015,7,26,8000)
    db.read('meituan', 'beijing', 2015,7,26)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值