Python
Hiidiot
这个作者很懒,什么都没留下…
展开
-
if __name__ == '__main__'
if __name__ == '__main__': # When you run for testing only 我们在测试一个模块时,经常会在模块顶层中,用print()进行输出,但是我们在import一个模块时,却不希望它顶层的print( )执行。这感觉并不好。 因此python就通过_name_来达到检测模块的设计意图。模块在测试时,被导入时都符合我们的要转载 2014-11-02 12:22:22 · 269 阅读 · 0 评论 -
python字典访问的三种方法
定义字典 dic = {'a':"hello",'b':"how",'c':"you"} 方法一: for key in dic: print key,dic[key] print key + str(dic[key]) 结果: a hello ahello c you cyou b how bhow 细节: print key,dic[k转载 2014-11-20 20:58:40 · 362 阅读 · 0 评论 -
使用Python读取和写入CSV文件
python中本身就自带csv模块,参考在线手册:http://docs.python.org/2/library/csv.html 1.用python读取csv文件: csv是逗号分隔符格式 一般我们用的execl生成的格式是xls和xlsx 直接重命名为csv的话会报错: Error: line contains NULL byte insun解决方案:出错原因是直接是把后缀转载 2014-11-27 18:01:36 · 622 阅读 · 0 评论 -
python读写文件
Python读写文件 1.open 使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。 file_object = open('thefile.txt') try: all_the_text = file_object.read( ) finally: file_object.close(转载 2014-11-27 16:52:05 · 261 阅读 · 0 评论