为什么那些教Python课程的人不用Python去做技术赚钱?

就像顶级的运动员们都会有很多训练师指导他们训练,训练师们具备专业知识、专业技能,却不会自己上场比赛,原因有二:其一是黄金年龄段已经过了,已经不能适应高强度训练;其二是他们拿到了该拿到的成绩,剩下的工作即是培养更多能拿成绩的人 ,而且相对轻松。教大家学Python也是同理。

咳咳,然后我们言归正传,普通小白学会Python到底具体能做什么呢?
Python是一种很高效的语言,是一种脚本语言,通过它能编程完成数据收集,还可以批量化自动操作简单任务,代替枯燥的手工操作,另外还能通过学习它,了解计算机的编程思维。
对于普通小白,肯定就不涉及复杂开发了,我们电脑中的文件,Word内容,Excel,都是数据,有了数据,自然就需要对数据进行存储、读写和分发。
#pgc-card .pgc-card-href { text-decoration: none; outline: none; display: block; width: 100%; height: 100%; } #pgc-card .pgc-card-href:hover { text-decoration: none; } /*pc 样式*/ .pgc-card { box-sizing: border-box; height: 164px; border: 1px solid #e8e8e8; position: relative; padding: 20px 94px 12px 180px; overflow: hidden; } .pgc-card::after { content: " "; display: block; border-left: 1px solid #e8e8e8; height: 120px; position: absolute; right: 76px; top: 20px; } .pgc-cover { position: absolute; width: 162px; height: 162px; top: 0; left: 0; background-size: cover; } .pgc-content { overflow: hidden; position: relative; top: 50%; -webkit-transform: translateY(-50%); transform: translateY(-50%); } .pgc-content-title { font-size: 18px; color: #222; line-height: 1; font-weight: bold; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .pgc-content-desc { font-size: 14px; color: #444; overflow: hidden; text-overflow: ellipsis; padding-top: 9px; overflow: hidden; line-height: 1.2em; display: -webkit-inline-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; } .pgc-content-price { font-size: 22px; color: #f85959; padding-top: 18px; line-height: 1em; } .pgc-card-buy { width: 75px; position: absolute; right: 0; top: 50px; color: #406599; font-size: 14px; text-align: center; } .pgc-buy-text { padding-top: 10px; } .pgc-icon-buy { height: 23px; width: 20px; display: inline-block; background: url(https://lf3-cdn-tos.bytescm.com/obj/cdn-static-resource/pgc/v2/pgc_tpl/static/image/commodity_buy_f2b4d1a.png); } Python高手修炼之道:数据处理与机器学习实战 ¥79 购买数据的存储、读写和分发一般有两大模式:数据库模式和数据文件模式。
最常见的数据文件类型是 Excel 表格和 CSV 文件,在科研领域,HDF 和 netCDF 也是常用的数据文件格式。
Python读写Excel文件
Python读写Excel文件可以做以下操作:
使用openpyxl模块读写XLSX格式文件使用xlrd模块读写XLS格式的文件使用xlwt模块生成XLS格式的文件Excel 文件有两种格式,分别对应 .xls 和 .xlsx 两种扩展名(XLS 格式和 XLSX 格式)。前者使用 97-2003 模板,是早期的文件格式,现在已经逐渐被后者所淘汰,但仍然会遇到 XLS 格式的数据文件需要处理。
openpyxl 模块专门用于读写 XLSX 格式的文件,xlrd 模块和 xlwt 模块则专门用于读写 XLS 格式的数据文件。这 3 个模块都可以使用 pip 命令安装。如果不需要处理 XLS 格式的数据文件,那么只需要安装 openpyxl 模块就可以了。

1.使用openpyxl 模块读写XLSX 格式的文件
Excel 文件的基本操作就是对文件(book)和工作表(sheet)进行的操作。使用 openpyxl 模块读写 Excel 文件,需要使用到 book 和 sheet 的概念。Openpyxl 模块使用 load_workbook( ) 函数将已有的 Excel 文件读成 book 对象,使用 workbook( ) 函数创建新的 book 对象,这两种方式得到的 book 对象都可以读写。
#pgc-card .pgc-card-href { text-decoration: none; outline: none; display: block; width: 100%; height: 100%; } #pgc-card .pgc-card-href:hover { text-decoration: none; } /*pc 样式*/ .pgc-card { box-sizing: border-box; height: 164px; border: 1px solid #e8e8e8; position: relative; padding: 20px 94px 12px 180px; overflow: hidden; } .pgc-card::after { content: " "; display: block; border-left: 1px solid #e8e8e8; height: 120px; position: absolute; right: 76px; top: 20px; } .pgc-cover { position: absolute; width: 162px; height: 162px; top: 0; left: 0; background-size: cover; } .pgc-content { overflow: hidden; position: relative; top: 50%; -webkit-transform: translateY(-50%); transform: translateY(-50%); } .pgc-content-title { font-size: 18px; color: #222; line-height: 1; font-weight: bold; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .pgc-content-desc { font-size: 14px; color: #444; overflow: hidden; text-overflow: ellipsis; padding-top: 9px; overflow: hidden; line-height: 1.2em; display: -webkit-inline-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; } .pgc-content-price { font-size: 22px; color: #f85959; padding-top: 18px; line-height: 1em; } .pgc-card-buy { width: 75px; position: absolute; right: 0; top: 50px; color: #406599; font-size: 14px; text-align: center; } .pgc-buy-text { padding-top: 10px; } .pgc-icon-buy { height: 23px; width: 20px; display: inline-block; background: url(https://lf3-cdn-tos.bytescm.com/obj/cdn-static-resource/pgc/v2/pgc_tpl/static/image/commodity_buy_f2b4d1a.png); } 人工智能简史 第2版 ¥66.2 购买下面的代码演示了如何使用 openpyxl 模块编辑 XLSX 格式的 Excel 文件。
>>> from openpyxl import load_workbook>>> wb = load_workbook(r"D:\NumPyFamily\data\ionosphere.xlsx")>>> wb.sheetnames['电离层']>>> sh = wb["电离层"] # 选择表>>> sh.max_row # 有效行数351>>> sh.max_column # 有效列数34>>> sh['C1'] # 返回C1单元格对象<Cell '电离层'.C1>>>> sh['C1'].value # 返回C1单元格内容0.99539>>> sh[1][2].value # 也可以这样指定单元格0.99539>>> sh['C1'].value = 99.99 # 修改单元格内容>>> wb.save(r"D:\NumPyFamily\data\ionosphere_demo.xlsx") # 保存文件下面的代码演示了如何使用 openpyxl 模块创建 XLSX 格式的 Excel 文件。
>>> from openpyxl import Workbook>>> wb = Workbook() # 创建book>>> sh0 = wb.active # 激活默认的sheet>>> sha = wb.create_sheet("成绩表") # 创建新表>>> shb = wb.create_sheet("收支表") # 创建新表>>> sha.append(['姓名','语文','数学']) # 可以在末尾追加一行>>> sha.append(['Alice',95,99])>>> sha['B2'] = 98 # 也可以单独写单元格>>> wb.sheetnames # 显示全部表名['Sheet', '成绩表', '收支表']>>> del wb['Sheet'] # 删除表>>> wb.save(r"D:\NumPyFamily\data\demo.xlsx") # 保存文件下面代码演示了如何使用 openpyxl 模块设置字体、单元格等的样式。
>>> from openpyxl import Workbook>>> from openpyxl.styles import Font, colors, Alignment>>> wb = Workbook()>>> sh = wb.active>>> f1 = Font(name='微软雅黑', size=16, italic=True, color=colors.BLACK, bold=True)>>> sh['A1'].font = f1 # 设置字体>>> align = Alignment(horizontal='center', vertical='center')>>> sh['B2'].alignment = align # 设置对齐方式>>> sh.row_dimensions[2].height = 24 # 设置第2行高度>>> sh.column_dimensions['C'].width = 20 # 设置C列宽度>>> sh.merge_cells('A3:C4') # 合并A3到C4的单元格2.使用 xlrd 模块读写 XLS 格式的文件
使用 xlrd 模块读写 Excel 文件的方法与使用 openpyxl 模块读写 Excel 文件非常类似。用 xlrd 模块打开一个 Excel 文件,返回的是一个 book 对象;使用 sheet 名或序号从 book 的数据表中选择一个 sheet,即可从中读取数据。
#pgc-card .pgc-card-href { text-decoration: none; outline: none; display: block; width: 100%; height: 100%; } #pgc-card .pgc-card-href:hover { text-decoration: none; } /*pc 样式*/ .pgc-card { box-sizing: border-box; height: 164px; border: 1px solid #e8e8e8; position: relative; padding: 20px 94px 12px 180px; overflow: hidden; } .pgc-card::after { content: " "; display: block; border-left: 1px solid #e8e8e8; height: 120px; position: absolute; right: 76px; top: 20px; } .pgc-cover { position: absolute; width: 162px; height: 162px; top: 0; left: 0; background-size: cover; } .pgc-content { overflow: hidden; position: relative; top: 50%; -webkit-transform: translateY(-50%); transform: translateY(-50%); } .pgc-content-title { font-size: 18px; color: #222; line-height: 1; font-weight: bold; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .pgc-content-desc { font-size: 14px; color: #444; overflow: hidden; text-overflow: ellipsis; padding-top: 9px; overflow: hidden; line-height: 1.2em; display: -webkit-inline-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; } .pgc-content-price { font-size: 22px; color: #f85959; padding-top: 18px; line-height: 1em; } .pgc-card-buy { width: 75px; position: absolute; right: 0; top: 50px; color: #406599; font-size: 14px; text-align: center; } .pgc-buy-text { padding-top: 10px; } .pgc-icon-buy { height: 23px; width: 20px; display: inline-block; background: url(https://lf3-cdn-tos.bytescm.com/obj/cdn-static-resource/pgc/v2/pgc_tpl/static/image/commodity_buy_f2b4d1a.png); } Python神经网络编程 ¥51.8 购买表 3-5 列出了 xlrd 模块的 book 对象的常用方法。

表 3-6 列出了 xlrd 模块的 sheet 对象的常用方法。

下面的代码演示了使用 xlrd 模块从 Excel 文件中读取数据的方法。
>>> import xlrd>>> book = xlrd.open_workbook(r"D:\NumPyFamily\data\ionosphere.xls")>>> book.sheet_names() # 获取全部表名['电离层']>>> sh = book.sheet_by_name('电离层') # 通过表名取得sheet对象>>> sh = book.sheet_by_index(0) # 通过索引取得sheet对象>>> sh.nrows # 有效行数351>>> sh.ncols # 有效列数34>>> sh.row_values(3, start_colx=3, end_colx=8) # 读取第3行的第3列到第8列的值 [-0.45161, 1.0, 1.0, 0.71216, -1.0]>>> sh.col_values(2, start_rowx=3, end_rowx=10) # 读取第2列的第3行到第10行的值 [1.0, 1.0, 0.02337, 0.97588, 0.0, 0.96355, -0.01864]>>> sh.cell_value(3,4) # 返回第3行第4列的值1.03.使用 xlwt 模块生成 XLS 格式的文件
使用 xlwt 模块只能生成新的 Excel 文件,不能对已有的 Excel 文件进行编辑。其使用方法与使用 xlrd 读取 Excel 文件有点类似,首先创建一个 book 对象,然后添加 sheet,并对 sheet 做写入操作。另外,xlwt 模块还提供了单元格、字体、边框等样式的设置方法。
当然,还可以利用 Python 读写其它格式的文件,例如HDF文件、CSV文件等,具体内容可参见《Python 高手修炼之道 数据处理与机器学习实战》。
以上就是简单数据文件读写的具体操作啦,无论是小白练手,还是白领办公都非常适用。
如果小白需要系统地学习Python然后将其应用,推荐你读一下《Python高手修炼之道》,这本书的作者许向武是CSDN的博客专家,资深程序员,使用Python超过10年,可以说对于Python的学习有系统的、独到的见解。

这本书系统地介绍了如何入门 Python 并利用 Python 进行数据处理与机器学习实战,从 Python 的基础安装开始介绍,系统梳理了 Python 的入门语法知识,归纳介绍了图像处理、数据文件读写、数据库操作等 Python 基本技能;然后详细讲解了 NumPy、Matplotlib、Pandas、Scipy、Scikit-learn 等在数据处理、机器学习领域的应用。

代码实例涵盖网络爬虫、数据处理、视觉识别、机器学习等应用领域,此外,作者还精心创作了语感训练100题、Python内置函数(类)手册、从新手到高手的100个模块等内容,以帮助读者更好地学习并掌握Python这一工具。

Python高手修炼之道:数据处理与机器学习实战
这本书非常适合初学者有针对性地进行编程技能提升,能够满足你从巩固基础、磨练技能再到拓展应用的学习需求,助你“修炼”为熟练的Python程序员。