当前位置:   article > 正文

python编程代码大全小海龟,python编程代码大全分享

python编程代码大全小海龟,python编程代码大全分享

这篇文章主要介绍了python编程代码大全 初学编程100个代码,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。

大家好,给大家分享一下python语言的代码书写规则有哪些,很多人还不知道这一点。下面详细解释一下python自学好学嘛。现在让我们来看看!

一些常用的python代码合集,方便检索引用

模块1:读写excel文件
  1. from datetime import datetime
  2. import odps
  3. import xlwt
  4. import os
  5. from odps import DataFrame
  6. import pandas as pd
  7. import xlrd
  8. import numpy as np
  9. from collections import defaultdict
  10. from collections import Counter
  11. # 写入工作簿
  12. def write_imf(fl_save_path, data):
  13. wb = xlwt.Workbook(encoding='utf-8') # 不写encoding会出现编码错误
  14. sh = wb.add_sheet(u'data', cell_overwrite_ok=True)
  15. # 表头部分,单独写
  16. colnames = data.columns.values
  17. for i in range(0, data.shape[1]):
  18. sh.write(0, i, colnames[i])
  19. # 表内容,循环写入,好像没简便的方法
  20. for i in range(1, len(data) + 1):
  21. for j in range(0, data.shape[1]):
  22. value = data.iloc[i - 1, j]
  23. # print(value)
  24. # 这里的坑特别多!!!数据读进来之后就成numpy.xxx64的类型了,在dataframe的时候就需要统一干掉!
  25. try:
  26. value.dtype
  27. if value.dtype == 'int64':
  28. value = int(value)
  29. # print('value is:%d,type is:%s'%(value,type(value)))
  30. if value.dtype == 'float64':
  31. value = float(value)
  32. # print('value is:%d,type is:%s' % (value, type(value)))
  33. except(RuntimeError, TypeError, NameError, ValueError, AttributeError):
  34. pass
  35. sh.write(i, j, value)
  36. wb.save(fl_save_path)
  37. print('congratulation save successful!')
  38. def save_pd_to_csv(fl_save_path, data):
  39. try:
  40. # 直接转csv不加encoding,中文会乱码
  41. data.to_csv(fl_save_path, encoding="utf_8_sig", header=True, index=False) # 存储
  42. return True
  43. except:
  44. return False
  45. def get_excel_content(file_path):
  46. # 获取excel内的SQL语句,需要通过xlrd获取workbook中的SQL内容,或者读txt,后续改为配置文件
  47. wb = xlrd.open_workbook(file_path, encoding_override='utf-8')
  48. sht = wb.sheet_by_index(0) # 默认第一个工作表
  49. # print(sht.name)
  50. wb_cont_imf = []
  51. nrows = sht.nrows # 行数
  52. wb_cont_imf = [sht.row_values(i) for i in range(0, nrows)] # 第一个工作表内容按行循环写入
  53. df = pd.DataFrame(wb_cont_imf[1:], columns=wb_cont_imf[0])
  54. return df
模块2:获取各种时间
  1. # 获取年月第一天最后一天
  2. def getMonthFirstDayAndLastDay(year=None, month=None):
  3. """
  4. :param year: 年份,默认是本年,可传int或str类型
  5. :param month: 月份,默认是本月,可传int或str类型
  6. :return: firstDay: 当月的第一天,datetime.date类型
  7. lastDay: 当月的最后一天,datetime.date类型
  8. """
  9. if year:
  10. year = int(year)
  11. else:
  12. year = datetime.date.today().year
  13. if month:
  14. month = int(month)
  15. else:
  16. month = datetime.date.today().month
  17. # 获取当月第一天的星期和当月的总天数
  18. firstDayWeekDay, monthRange = calendar.monthrange(year, month)
  19. # 获取当月的第一天
  20. firstDay = datetime.date(year=year, month=month, day=1)
  21. lastDay = datetime.date(year=year, month=month, day=monthRange)
  22. # return firstDay, lastDay
  23. return lastDay
模块3:pd中的dataframe转png
  1. # dataframe2png
  2. def render_mpl_table(data, col_width=5.0, row_height=0.625, font_size=1,
  3. header_color='#40466e', row_colors=['#f1f1f2', 'w'], edge_color='w',
  4. bbox=[0, 0, 1, 1], header_columns=0,
  5. ax=None,**kwargs):
  6. if ax is None:
  7. # size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array([col_width, row_height])
  8. # fig, ax = plt.subplots(figsize=size)
  9. fig, ax = plt.subplots() # 创建一个空的绘图区
  10. # 衍生知识点,服务器上安装中文字体
  11. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
  12. # plt.rcParams['font.sans-serif'] = ['WenQuanYi Zen Hei Mono']
  13. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
  14. plt.style.use('ggplot')
  15. ax.axis('off')
  16. mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, **kwargs)
  17. mpl_table.auto_set_font_size(False)
  18. mpl_table.set_fontsize(font_size)
  19. for k, cell in six.iteritems(mpl_table._cells):
  20. cell.set_edgecolor(edge_color)
  21. nrow = k[0]
  22. ncol = k[1]
  23. # 设置表格底色
  24. if nrow == 0 or ncol < header_columns:
  25. cell.set_text_props(weight='bold', color='w')
  26. cell.set_facecolor(header_color)
  27. else:
  28. cell.set_facecolor(row_colors[k[0] % len(row_colors)])
  29. # # 对当日异常数据为0的部分,着重体现
  30. # row_num = []
  31. # for k, cell in mpl_table._cells.items():
  32. # nrow = k[0]
  33. # ncol = k[1]
  34. # val = cell.get_text().get_text()
  35. # if nrow > 0 and ncol == 2 and val != '0':
  36. # row_num.append(nrow)
  37. # for k, cell in six.iteritems(mpl_table._cells):
  38. # nrow = k[0]
  39. # # 设置表格底色
  40. # if nrow in row_num:
  41. # cell.set_facecolor('gold')
  42. # 保留原图的设置
  43. # fig.set_size_inches(width/100.0,height/100.0)#输出width*height像素
  44. plt.gca().xaxis.set_major_locator(plt.NullLocator())
  45. plt.gca().yaxis.set_major_locator(plt.NullLocator())
  46. plt.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0)
  47. plt.margins(0, 0)
  48. return ax
模块4:绘制词云
  1. #!/user/bin/python
  2. # -*- coding:utf-8 -*-
  3. _author_ = 'xisuo'
  4. import datetime
  5. import calendar
  6. import xlwt
  7. import os
  8. import pandas as pd
  9. import xlrd
  10. import openpyxl
  11. import numpy as np
  12. from collections import defaultdict
  13. import platform
  14. from wordcloud import WordCloud,STOPWORDS
  15. import matplotlib.pyplot as plt
  16. from PIL import Image
  17. def create_wordcloud(docs=None,imgs=None,filename=None):
  18. '''
  19. :param docs:读入词汇txt,尽量不重复
  20. :param imgs: 读入想要生成的图形,网上随便找
  21. :param filename: 保存图片文件名
  22. :return:
  23. '''
  24. # Read the whole text.
  25. text = open(os.path.join(current_file, docs)).read()
  26. alice_mask = np.array(Image.open(os.path.join(current_file, imgs)))
  27. print(font_path)
  28. wc = WordCloud(background_color="white",
  29. max_words=2000,
  30. font_path=font_path, # 设置字体格式,如不设置显示不了中文
  31. mask=alice_mask,
  32. stopwords=STOPWORDS.add("said")
  33. )
  34. # generate word cloud
  35. wc.generate(text)
  36. # store to file
  37. if filename is None:filename="词云结果.png"
  38. wc.to_file(os.path.join(current_file, filename))
  39. def main():
  40. docs='demo.txt' #读入的文本
  41. imgs="eg.jpg" #需要绘制的图像
  42. filename='res_eg.png' #保存图片文件名
  43. create_wordcloud(docs=docs,imgs=imgs,filename=filename)
  44. print('create wordcloud successful')
  45. if __name__ == '__main__':
  46. start_time = datetime.datetime.now()
  47. print('start running program at:%s' % start_time)
  48. systemp_type = platform.system()
  49. if (systemp_type == 'Windows'):
  50. plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
  51. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
  52. font_path='simfang.ttf'
  53. try:
  54. current_path = os.getcwd()
  55. except:
  56. current_path = os.path.dirname(__file__)
  57. current_file = os.path.join(current_path, 'docs')
  58. current_file = current_path
  59. elif (systemp_type == 'Linux'):
  60. font_path = 'Arial Unicode MS.ttf'
  61. plt.rcParams['font.family'] = ['Arial Unicode MS'] # 用来正常显示中文标签
  62. plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
  63. current_file = '/home/xisuo/mhc_work/docs/' # 服务器上的路径
  64. else:
  65. quit()
  66. if not os.path.exists(current_file):
  67. os.mkdir(current_file)
  68. print('目录中部存在docs文件夹,完成新文件夹创建过程。')
  69. print('当前操作系统:%s,文件存储路径为:%s' % (systemp_type, current_file))
  70. main()
  71. end_time = datetime.datetime.now()
  72. tt = end_time - start_timepython
  73. print('ending time:%s', end_time)
  74. print('this analysis total spend time:%s' % tt.seconds)
模块5:下载ppt素材
  1. #!/user/bin/python
  2. #-*- coding:utf-8 -*-
  3. _author_ = 'xisuo'
  4. import urllib.request
  5. import requests
  6. from bs4 import BeautifulSoup
  7. from lxml import etree
  8. import os
  9. url='http://www.pptschool.com/1491.html'
  10. response=requests.get(url).text
  11. # soup=BeautifulSoup(response,'lxml')
  12. # cont=soup.find('article', class_='article-content')
  13. html=etree.HTML(response)
  14. src_list=html.xpath('//div/article/p/img/@src')
  15. current_path=os.path.dirname(__file__)
  16. save_path=os.path.join(current_path,'ppt_img')
  17. if os.path.exists(save_path):
  18. pass
  19. else:
  20. os.mkdir(save_path)
  21. print('img folder create successful')
  22. i=1
  23. for src in src_list:
  24. save_img_path=os.path.join(save_path,'%d.jpg'%i)
  25. try:
  26. with open(save_img_path,'wb') as f:
  27. f.write(urllib.request.urlopen(src).read())
  28. f.close()
  29. i=i+1
  30. print('save true')
  31. except Exception as e:
  32. print('save img fail')
模块6:模型存储和读取
  1. rom sklearn import joblib
  2. from sklearn import svm
  3. from sklearn2pmml import PMMLPipeline, sklearn2pmml
  4. import pickle
  5. def save_model(train_X,train_y):
  6. ''''
  7. save model
  8. :return:
  9. '''
  10. clf = svm.SVC()
  11. clf.fit(X, y)
  12. joblib.dump(clf, "train_model.m")
  13. sklearn2pmml(clf, "train_model.pmml")
  14. with open('train_model.pickle', 'wb') as f:
  15. pickle.dump(clf, f)
  16. return True
  17. def load_model():
  18. '''
  19. laod model
  20. :return:
  21. '''
  22. clf_joblib=joblib.load('train_model.m')
  23. clf_pickle== pickle.load(open('linearregression.pickle','rb'))
  24. return clf_joblib,clf_pickle
模块7:TF-IDF
  1. import time
  2. import pandas as pd
  3. import numpy as np
  4. from sklearn.feature_extraction.text import CountVectorizer
  5. from sklearn.feature_extraction.text import TfidfVectorizer
  6. # 读取数据 - 性能不好待优化
  7. print('开始读取KeyTag标签...')
  8. read_data_path = 'D:/untitled/incomelevel_kwtag_20190801.txt'
  9. load_data = pd.read_csv(read_data_path, sep='\t',encoding='utf-8')
  10. data = pd.DataFrame(load_data,columns = ['income_level','kw_tag'])
  11. print('...读取KeyTag标签完成')
  12. # 将数据分组处理
  13. print('开始分组处理KeyTag标签...')
  14. # 高收入
  15. incomelevel_top = data[data['income_level'] == '高']
  16. incomelevel_top = incomelevel_top.head() #test
  17. kw_tag_top = ' '.join(incomelevel_top['kw_tag'])
  18. print('kw_tag_top : \n',kw_tag_top)
  19. # 中收入
  20. incomelevel_mid = data[data['income_level'] == '中']
  21. incomelevel_mid = incomelevel_mid.head() #test
  22. kw_tag_mid = ' '.join(incomelevel_mid['kw_tag'])
  23. print('kw_tag_mid : \n',kw_tag_mid)
  24. # 低收入
  25. incomelevel_low = data[data['income_level'] == '低']
  26. incomelevel_low = incomelevel_low.head() #test
  27. kw_tag_low = ' '.join(incomelevel_low['kw_tag'])
  28. print('kw_tag_low : \n',kw_tag_low)
  29. print('...分组处理KeyTag标签完成')
  30. # 开始加载TF-IDF
  31. vectorizer = CountVectorizer()
  32. result = vectorizer.fit_transform([kw_tag_top, kw_tag_mid, kw_tag_low])
  33. transformer = TfidfVectorizer()
  34. kw_tag_score = transformer.fit_transform([kw_tag_top, kw_tag_mid, kw_tag_low])
  35. print('...KeyTag分词结束')
  36. # 获取全量标签
  37. kw_tag_value = transformer.get_feature_names()
  38. result_target = pd.DataFrame(kw_tag_value,columns = ['kw_tag'])
  39. print('result_target : \n',result_target)
  40. # 分词得分处理
  41. tf_score = kw_tag_score.toarray()
  42. print('tf_score : \n',tf_score)
  43. kw_tag_score_top = pd.DataFrame(tf_score[0],columns = ['kw_tag_score_top']) # 217
  44. kw_tag_score_mid = pd.DataFrame(tf_score[1],columns = ['kw_tag_score_mid'])
  45. kw_tag_score_low = pd.DataFrame(tf_score[2],columns = ['kw_tag_score_low'])
  46. print(len(kw_tag_score_top))
模块8:生成省市地图
  1. import time
  2. import pandas as pd
  3. import xlrd
  4. import re
  5. import matplotlib.pyplot as plt
  6. import six
  7. import numpy as np
  8. # 载入ppt和pyecharts相关的包
  9. from pyecharts.render import make_snapshot
  10. from snapshot_phantomjs import snapshot
  11. from pyecharts import options as opts
  12. from collections import defaultdict
  13. from pyecharts.charts import Bar, Geo, Map, Line,Funnel,Page
  14. import os
  15. from example.commons import Faker
  16. def create_zjs_map():
  17. folder_path = os.getcwd()
  18. file_name = "白皮书数据地图.xlsx"
  19. file_path = os.path.join(folder_path, file_name)
  20. dat = get_excel_content(file_path, sheet_name="省份地图")
  21. df = dat[['城市', '渗透率']]
  22. df.columns = ['city', 'penarate']
  23. print(df)
  24. # df['city'] = df['city'].apply(lambda x: reg.sub('', x))
  25. citys = df['city'].values.tolist()
  26. values = df['penarate'].values.tolist()
  27. print(citys)
  28. print('{:.0f}%'.format(max(values)*100),'{:.0f}%'.format(min(values)*100))
  29. city_name='浙江'
  30. penetration_map = (
  31. Map(init_opts=opts.InitOpts(width='1200px', height='1000px', bg_color='white'))
  32. .add("{}透率分布".format(city_name), [list(z) for z in zip(citys, values)], city_name)
  33. .set_series_opts(
  34. label_opts=opts.LabelOpts(
  35. is_show=True,
  36. font_size=15
  37. )
  38. )
  39. .set_global_opts(
  40. visualmap_opts=opts.VisualMapOpts(
  41. is_show=True,
  42. max_=max(values),
  43. min_=min(values),
  44. is_calculable=False,
  45. orient='horizontal',
  46. split_number=3,
  47. range_color=['#C2D5F8', '#88B0FB', '#4D8AFD'],
  48. range_text=['{:.0f}%'.format(max(values)*100),'{:.0f}%'.format(min(values)*100)],
  49. pos_left='10%',
  50. pos_bottom='15%'
  51. ),
  52. legend_opts=opts.LegendOpts(is_show=False)
  53. )
  54. )
  55. # penetration_map.render()
  56. make_snapshot(snapshot, penetration_map.render(), "zj_map.png")
  57. print('保存 zj_map.png')
  58. return penetration_map
  59. def create_county_map(city_name):
  60. folder_path = os.getcwd()
  61. file_name = "白皮书数据地图.xlsx"
  62. file_path = os.path.join(folder_path, file_name)
  63. dat = get_excel_content(file_path, sheet_name="城市地图")
  64. df = dat[['city', 'county', 'penarate']][dat.city == city_name]
  65. citys = df['county'].values.tolist()
  66. values = df['penarate'].values.tolist()
  67. max_insurance = max(values)
  68. print(citys)
  69. province_penetration_map = (
  70. Map(init_opts=opts.InitOpts(width='1200px', height='1000px', bg_color='white'))
  71. .add("{}透率分布".format(city_name), [list(z) for z in zip(citys, values)], reg.sub('',city_name))
  72. .set_series_opts(
  73. label_opts=opts.LabelOpts(
  74. is_show=True,
  75. font_size=15
  76. )
  77. )
  78. .set_global_opts(
  79. visualmap_opts=opts.VisualMapOpts(
  80. is_show=True,
  81. max_=max(values),
  82. min_=min(values),
  83. is_calculable=False,
  84. orient='horizontal',
  85. split_number=3,
  86. range_color=['#C2D5F8', '#88B0FB', '#4D8AFD'],
  87. range_text=['{:.0f}%'.format(max(values) * 100), '{:.0f}%'.format(min(values) * 100)],
  88. pos_left='10%',
  89. pos_bottom='5%'
  90. ),
  91. legend_opts=opts.LegendOpts(is_show=False)
  92. )
  93. )
  94. # insurance_map.render()
  95. make_snapshot(snapshot, province_penetration_map.render(), "city_map_{}.png".format(city_name))
  96. print('保存 city_map_{}.png'.format(city_name))
  97. return province_penetration_map
  98. def create_funnel_label():
  99. folder_path=os.getcwd()
  100. file_name = "白皮书数据地图.xlsx"
  101. file_path = os.path.join(folder_path, file_name)
  102. dat = get_excel_content(file_path, sheet_name="漏斗图")
  103. df = dat[['category', 'cnt']]
  104. print(df)
  105. category = df['category'].values.tolist()
  106. values = df['cnt'].values.tolist()
  107. funnel_map = (
  108. Funnel(init_opts=opts.InitOpts(width='1200px', height='1000px', bg_color='white'))
  109. .add("漏斗图", [list(z) for z in zip(category, values)])
  110. .set_series_opts(
  111. label_opts=opts.LabelOpts(
  112. position='inside',
  113. font_size=16,
  114. )
  115. )
  116. .set_global_opts(
  117. legend_opts=opts.LegendOpts(is_show=False)
  118. )
  119. )
  120. # insurance_map.render()
  121. make_snapshot(snapshot, funnel_map.render(), "funnel.png")
  122. print('保存 funnel.png')
  123. return funnel_map
  124. city_list=['温州市','杭州市','绍兴市','嘉兴市','湖州市','宁波市','金华市','台州市','衢州市','丽水市','舟山市']
  125. for city_name in city_list:
  126. create_county_map(city_name)
关于Python学习资料:

在学习python中有任何困难不懂的可以微信扫描下方CSDN官方认证二维码加入python交流学习
多多交流问题,互帮互助,这里有不错的学习教程和开发工具Deepl降重

python兼职资源+python全套学习资料

一、Python所有方向的学习路线

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
在这里插入图片描述

二、Python必备开发工具

在这里插入图片描述

四、Python视频合集

观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
在这里插入图片描述

五、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。在这里插入图片描述

六、Python练习题

检查学习结果。
在这里插入图片描述

七、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
在这里插入图片描述
在这里插入图片描述
最后,千万别辜负自己当时开始的一腔热血,一起变强大变优秀。

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树首页概览393669 人正在系统学习中
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/116931
推荐阅读
相关标签
  

闽ICP备14008679号