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)
相关文章推荐
- 如何调用另一个python文件中的代码
- [python]小技巧集锦
- Python程序的执行原理
- python中出现IndentationError:unindent does not match any outer indentation level错误
- Django+python实现网页数据的excel导出
- python学习 - yield
- Alternating Least Squares(ASL) for Implicit Feedback Datasets的数学推导以及用Python实现
- ubuntu python 安装
- python tab补全
- python数组过滤实现方法
- Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
- Python去除多余空格
- 【剑指offer】Q32:从1至n整1出现的次数(python)
- Python中的re.search和re.findall之间的区别2
- Python多线程结合队列下载百度音乐的方法
- python安装
- Google's Python Class(六)——Python 正则表达式
- 使用python求字符串或文件的MD5
- 关于python在cmd中输出中文乱码的问题
- 用Python进行自然语言处理-2. Accessing Text Corpora and Lexical Resources