
Python
Anntonnia
这个作者很懒,什么都没留下…
展开
-
Jupyter Notebook运行Python代码如何传参
这样,在后续代码中, .py 和 .ipynb源代码一致,无需为在Jupyter Notebook中运行而进行修改。在Jupyter Notebook中,运行Python源代码非常方便,但是如何模拟命令行方式运行时的输入参数呢?如果直接使用sys.argv会出现错误。argv[1]为 test.txt。因此,解决方案就是对。原创 2024-03-04 16:38:40 · 861 阅读 · 0 评论 -
Python开发简单爬虫课程源代码及解析
Python开发简单爬虫徐老师的课程“Python开发简单爬虫”(链接:https://www.imooc.com/learn/563)思路清晰,步骤详细,讲解细致,是非常好的Python爬虫开发入门课程。美中不足的是随着时间推移,课程中的有些库更新了,百度百科词条的url格式也变了,如果完全按照课程的内容开发代码是不能正常工作的。下面的代码是经过修改后的代码,而且为便于初学者阅读,添加了详细注释。共5个源代码文件:spider_main.py, url_manager.py, html_dow原创 2020-09-15 11:12:31 · 935 阅读 · 0 评论 -
解决Python UnicodeEncodeError: ‘gbk‘ codec can‘t encode...
使用Python写文件的时候,或者将网络数据流写入到本地文件的时候,大部分情况下会遇到:UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position ... 这个问题。 网络上有很多类似的文件讲述如何解决这个问题,但是无非就是encode,decode相关的,这是导致该问题出现的真正原因吗?不是的。 很多时候,我们使用了decode和encode,试遍了各种编码,utf8,utf-8,gbk,gb2312等等,该有的编码都转载 2020-09-15 08:40:23 · 1175 阅读 · 0 评论 -
Python包及Jupyter Notebook安装
Python包:包即文件夹,包含__init__.py文件(标识当前文件夹是一个包) 是一个分层次的目录结构,定义了一个由模块及子包和子包下的子包等组成的Pythone的应用环境 例如:test.py package-runoob |----__init__.py |----runoob1.p...原创 2019-11-18 14:46:55 · 548 阅读 · 1 评论 -
Head First Python--使用类封装数据和方法,类继承简化编码
import os#设置工作路径,包含测试需要的运动员数据文件os.chdir('d:/Python_test/ch6') #数据整理函数:数据文件中可能存在2-34、2.34、2:34等不同的记录def sanitize(time_string): if '-' in time_string: splitter = '-' elif ':' in ...原创 2018-07-20 09:25:34 · 275 阅读 · 0 评论 -
Head First Python--读入字符串文件、替换、排序、去重以及使用函数 --使用函数进一步简化代码
import osimport pickleimport nesteros.chdir('d:/Python_test/ch5')def sanitize(time_string): if '-' in time_string: splitter = '-' elif ':' in time_string: splitter = ':'...原创 2018-07-19 16:12:13 · 343 阅读 · 0 评论 -
Head First Python--读入字符串文件、替换、排序、去重以及使用函数
import osimport pickleimport nesteros.chdir('d:/Python_test/ch5')def sanitize(time_string): if '-' in time_string: splitter = '-' elif ':' in time_string: splitter = ':'...原创 2018-07-19 16:05:05 · 314 阅读 · 0 评论 -
Head First Python--sketch.txt: for test
Man: Is this the right room for an argument?Other Man: I've told you once.Man: No you haven't!Other Man: Yes I have.Man: When?Other Man: Just now.Man: No you didn't!Other Man: Yes I did!Man: Y...原创 2018-07-18 22:31:31 · 1635 阅读 · 0 评论 -
Head First Python--test for pickle load
import osimport pickleimport nesteros.chdir('d:/Python_test')new_man = []new_other = []try: with open('man.txt', 'rb') as man_file: new_man = pickle.load(man_file) with open('ot...原创 2018-07-18 22:29:56 · 214 阅读 · 0 评论 -
Head First Python--test for pickle dump
import osimport pickleimport nesteros.chdir('d:/Python_test')man = []other = []try: with open('sketch.txt') as data: for each_line in data: try: (role, l...原创 2018-07-18 22:28:41 · 212 阅读 · 0 评论 -
Head First Python--写文件 with nester
import osimport nesteros.chdir('d:/Python_test')man = []other = []try: with open('sketch.txt') as data: for each_line in data: try: (role, line_spoken) = ...原创 2018-07-18 22:10:01 · 274 阅读 · 1 评论 -
Head First Python--写文件
import osos.chdir('d:/Python_test')man = []other = []try: with open('sketch.txt') as data: for each_line in data: try: (role, line_spoken) = each_line.split...原创 2018-07-18 22:08:46 · 267 阅读 · 0 评论 -
Head First Python--进一步测试try-except: 不同的异常类型
import osos.chdir('d:/Python_test')try: with open('sketch.txt') as data: for each_line in data: try: (role, line_spoken) = each_line.split(':') ...原创 2018-07-18 22:06:55 · 207 阅读 · 0 评论 -
Head First Python--test for try-except
import osos.getcwd()os.chdir('d:/Python_test')os.getcwd()'''data = open('sketch.txt')#print(data.readline(), end='')try: for each_line in data: (role, line_spoken) = each_line.split...原创 2018-07-18 22:07:03 · 177 阅读 · 0 评论 -
Head First Python--module: nester 生成文件: setup.py
from distutils.core import setupsetup( name = 'nester', version = '1.2.0', py_modules = ['nester'], author = 'hfpython', author_email = 'hfpython@headfirstlabs.com', url = 'h...原创 2018-07-18 22:07:13 · 217 阅读 · 0 评论 -
Head First Python--module: nester.py
"""This is the "nester.py" module, and it provides one function called print_lol()which prints lists that may or may not include nested lists."""import sysdef print_lol(the_list, indent=False, le...原创 2018-07-18 22:07:24 · 188 阅读 · 0 评论