您的位置:首页 > 编程语言 > Python开发

python学习笔记(一)

2015-07-27 20:27 696 查看

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: