你写过哪些实用的Python代码?

yizhihongxing

Python这门语言很适合用来写些实用的小脚本,跑个自动化、爬虫、算法什么的,非常方便。

 

这也是很多人学习Python的乐趣所在,可能只需要花个礼拜入门语法,就能用第三方库去解决实际问题。我在Github上就看到过不少Python代码的项目,几十行代码就能实现一个场景功能,非常实用。

 

比方说仓库Python-master里的几个简单例子:

 

1、创建二维码

 

import pyqrcode
import png
from pyqrcode import QRCode

# Text which is to be converted to QR code
print("Enter text to convert")
s = input(": ")
# Name of QR code png file
print("Enter image name to save")
n = input(": ")
# Adding extension as .pnf
d = n + ".png"
# Creating QR code
url = pyqrcode.create(s)
# Saving QR code as  a png file
url.show()
url.png(d, scale=6)

 

 

2、从图片中截取文字

 

作者:朱卫军
链接:https://www.zhihu.com/question/282627359/answer/2521994250
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

# extract text from a img and its coordinates using the pytesseract module
import cv2
import pytesseract

# You need to add tesseract binary dependency to system variable for this to work

img = cv2.imread("img.png")
# We need to convert the img into RGB format
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

hI, wI, k = img.shape
print(pytesseract.image_to_string(img))
boxes = pytesseract.image_to_boxes(img)
for b in boxes.splitlines():
    b = b.split(" ")
    x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
    cv2.rectangle(img, (x, hI - y), (w, hI - h), (0, 0, 255), 0.2)

cv2.imshow("img", img)
cv2.waitKey(0)

3、判断闰年

 

def is_leap(year):
    leap = False
    if year % 4 == 0:
        leap = True
        if year % 100 == 0:
            leap = False
            if year % 400 == 0:
                leap = True
    return leap


year = int(input("Enter the year here: "))
print(is_leap(year))

4、简易日历

 

作者:朱卫军
链接:https://www.zhihu.com/question/282627359/answer/2521994250
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

from tkinter import *
import calendar

root = Tk()
# root.geometry("400x300")
root.title("Calendar")

# Function

def text():
    month_int = int(month.get())
    year_int = int(year.get())
    cal = calendar.month(year_int, month_int)
    textfield.delete(0.0, END)
    textfield.insert(INSERT, cal)


# Creating Labels
label1 = Label(root, text="Month:")
label1.grid(row=0, column=0)

label2 = Label(root, text="Year:")
label2.grid(row=0, column=1)

# Creating spinbox
month = Spinbox(root, from_=1, to=12, width=8)
month.grid(row=1, column=0, padx=5)

year = Spinbox(root, from_=2000, to=2100, width=10)
year.grid(row=1, column=1, padx=10)

# Creating Button
button = Button(root, text="Go", command=text)
button.grid(row=1, column=2, padx=10)

# Creating Textfield
textfield = Text(root, width=25, height=10, fg="red")
textfield.grid(row=2, columnspan=2)


root.mainloop()

 

 你写过哪些实用的Python代码?

 

 

5、打印图片分辨率

 

作者:朱卫军
链接:https://www.zhihu.com/question/282627359/answer/2521994250
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

def jpeg_res(filename):
   """"This function prints the resolution of the jpeg image file passed into it"""

   # open image for reading in binary mode
   with open(filename,'rb') as img_file:

       # height of image (in 2 bytes) is at 164th position
       img_file.seek(163)

       # read the 2 bytes
       a = img_file.read(2)

       # calculate height
       height = (a[0] << 8) + a[1]

       # next 2 bytes is width
       a = img_file.read(2)

       # calculate width
       width = (a[0] << 8) + a[1]

   print("The resolution of the image is",width,"x",height)

jpeg_res("img1.jpg")

这个项目只是作者平时工作用到的一些小脚本,可能也会帮助到你。作者虽然不是程序员,但他这种用代码解决问题的习惯会极大的提升效率,也会迸发出更多的创新思维。我觉得这样的代码每个人都可以写出来,只要慢慢积累多练习就可以。

 

 
 
 
 

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:你写过哪些实用的Python代码? - Python技术站

(0)
上一篇 2023年4月2日
下一篇 2023年4月2日

相关文章

  • 【pygame游戏】用Python实现一个蔡徐坤大战篮球的小游戏,可还行?【附源码】

    准备工作开发环境 Python版本:3.7.8 相关模块:requests模块;tqdm模块;pyfreeproxy模块;pyechats模块;以及一些python自带的模块。 效果预览开始界面   游戏规则 wasd 控制人物的移动,空格启动律师函炸毁全部篮球。            代码实现 导入模块 import pygame import sys i…

    2023年4月2日
    00
  • python的time库详解

    time库的使用:Python中内置了一些与时间处理相关的库,如time、datatime和calendar库。 其中time库是Python中处理时间的标准库,是最基础的时间处理库。 time库的功能如下: (1)计算机时间的表达 (2)提供获取系统时间并格式化输出功能 (3)提供系统级精确计时功能,用于程序性能分析 格式如下: import time t…

    2023年4月2日
    00
  • python进行敏感性分析(SALib库)

    什么是敏感性分析  敏感性分析(sensitivity analysis)是指从定量分析的角度研究有关因素发生某种变化对某一个或一组关键指标影响程度的一种不确定分析技术。每个输入的灵敏度用某个数值表示即敏感性指数(sensitivity index) 敏感性指数包括以下几种: 一阶指数:度量单个模型输入对输出方差的贡献 二阶指数:度量两个模型输入的相互作用对…

    2023年4月2日
    00
  • python自学最快多长时间学完?

    0.基本的数据类型:   Number(数字) String(字符串) List(列表) Tuple(元组) Set(集合) Dictionary(字典)   1.基本的输入输出: input #输入 print #输出 age = 10; name = “小明”; print(age,type(age)) inputage = input(“请输入你的年龄…

    Python开发 2023年4月2日
    00
  • 这个Python读取文件的方法,堪称天花板级别…

    1、方法介绍基本用法 先来看一下fileinput的基本功能: fileinput.filename():返回当前被读取的文件名。—>在第一行被读取之前,返回 None。 fileinput.fileno():返回以整数表示的当前文件“文件描述符”。—>当未打开文件时(处在第一行和文件之间),返回 -1。 fileinput.lineno():返…

    2023年4月2日
    00
  • python常见报错以及解决方案,,以下是Python常见的报错以及解决方法,快进入收藏…

    AttribteError: ‘module’ object has no attribute xxx’ 描述:模块没有相关属性。可能出现的原因:1.命名.py文件时,使用了Python保留字或者与模块名等相同。解决:修改文件名2…pyc文件中缓存了没有更新的代码。解决:删除该库的.pyc 文件   AttributeError: ‘Obj’ object …

    Python开发 2023年4月2日
    00
  • Python中的列表条件求和方法

    列表条件求和方法   list_data=[ [1.0, ‘配件’, ‘522422’, ‘铝扣板用纽扣’, ‘金色’, ”, 72.0, ‘PC’, ”], [2.0, ‘配件’, ‘500031’, ‘十字槽沉头自钻自攻螺钉4.2*45’, ‘原色’, ”, 72.0, ‘PC’, ”], [1.0, ‘配件’, ‘522422’, ‘铝扣板用纽…

    Python开发 2023年4月2日
    00
  • 程序员必备的6个好习惯,成为更优秀的自己

    如果你有机会跟一些技术大牛接触的话,你会发现别人不仅是技术上比一般人强很多,而且在做事方面也有许多不一样的习惯,在职场卷了这么多年依然保持的习惯,往往是值得我们借鉴和学习的。 今天给大家分享几个优秀程序员的好习惯,养成这6个习惯,你也能成为编程老司机。       第一,代码自测再交付 写完代码不要急于交付,先把代码自己测试一遍,过了自己这一关,减少别人发现…

    2023年4月2日
    00
合作推广
合作推广
分享本页
返回顶部