IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> 工作遇到的一些批量处理问题的Python解决方案 -> 正文阅读

[Python知识库]工作遇到的一些批量处理问题的Python解决方案

感叹一下,Python真的是神器,语言风格好上手,可深可浅,话不多说,进入正题

首先是传统艺能--------“搭建一个开发环境”,本次使用的win7 64位,Python版本3.8.10,一路默认,安装器提供将安装路径设为环境变量的功能,非常nice,使用pycharm-community2021.1.3

首先依然是传统艺能-------hello world,pycharm为我们提供这个功能,只要新建目录,新建工程,生成main.py里面会包含Hi python的demo

案例一:

多文件之间的调用

在同一路径下新建hello_world.py

内容如下

def print_hello(name):
????print(f'hello {name}')

在main.py中可以这么调用

from hello_world import print_hello#引入函数



if __name__ == '__main__':

    print_hello('world')

简单快捷明了,点击运行,观察结果

案例二:

熟悉之后很快就遇到了我的第一个用到的地方,我需要从一个.c文件中导出该文件中一些字符;

该文件定义了一组指向字符串常量的指针,需要提取这些指针的名称作为Excel的第一列

该文件在不同的情况下,将这些指针指向不同的字符串常量,需要分列将每一个情况下指针指向的这些字符串记录下来。

talk is cheap,show you my code

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

#使用openpyxl模块管理Excel
from hello_world import print_hello
import openpyxl

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

#判断字符是否为中文
def is_chinese(string):
    for ch in string:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True
    return False

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    print_hello('world')

    #字符列表
    #此处添加存储位置
    char_list = []
    char_list_chinese = []
    char_list_english = []

    f = open('xxxxxx.c', 'r', encoding='utf-8')
    lines = f.readlines()
    # print(lines)

    wb = openpyxl.Workbook()
    ws = wb.active
    ws.title = 'char_list'


    for line in lines:
        #line.strip用于分离行两端的多余空格,制表符等
        line = line.strip()
        #使用*STRING_作为指向字符串常量的指针的名称的同一开头
        if '*STRING_' in line:

            #主要使用find寻找特定字符
            find_char = line[line.find('*')+1:line.find(';')]

            #list.count(char)统计char在list中出现的次数
            if char_list.count(find_char) == 0:
               #list.append(char)将char添加至list末尾
               char_list.append(find_char)

            #print(find_char)

    num = 0
    for list in char_list:
        #print(list)
        #先插入空格,否则会出现操作空列表的情况
        char_list_chinese.insert(num, ' ')
        char_list_english.insert(num, ' ')
        for line in lines:
            #line = line.strip()
            #在行中寻找变量名称 去除带const char *的,此为定义
            if (list in line) and ("const char *" not in line):
                find_char = line[line.find('"'):line.find(';')]
                #if char_list_chinese.count(find_char) == 0 and char_list_english.count(find_char) == 0 :
                if (is_chinese(find_char)):
                    char_list_chinese[num] = find_char
                    #print("chinese",find_char)
                else:
                    char_list_english[num] = find_char
                    #print("english",find_char)

                    #if char_list_chinese[num] == ' ' or char_list_english[num] == ' ' :
                        #print('searching')
                        #pass
                    #else:
        print(num,char_list[num], char_list_chinese[num], char_list_english[num])
        
        #按格式写入excel中,主要使用cell(行,列).value=值
        ws.cell(num+1,1).value =   char_list[num]
        ws.cell(num+1,2).value = char_list_chinese[num]
        ws.cell(num+1,3).value = char_list_english[num]
        num = num + 1


    f.close()

    print(len(char_list),len(char_list_chinese),len(char_list_english))




    #保存excel
    wb.save('cases.xlsx')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/

案例三:

和案例二大同小异,用于在一个文件中提取出所有“ ”中间的字符,并将字符添加到此文件末尾

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

    char_list = []

    f = open('GBK字库', 'a+', encoding='utf-8')
    f.seek(0, 0)
    lines = f.readlines()
    for line in lines:
        line = line.strip()
        find_char = line[line.find('{') + 2:line.find('{') + 3]
        if char_list.count(find_char) == 0:
            char_list.append(find_char)
            #f.write(find_char)
            print(find_char)

    f.close()

    f = open('GBK字库', 'a+', encoding='utf-8')
    for list in char_list:
        f.write(list)
    f.close()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/

案例四:

此案例是一个批处理的案例,在svn的备份中出现了将所有的文件夹和文件都多出了一个同名+(序号)名称的文件或文件夹,需要删除,代码主要是遍历,这里使用的是os模块中的walk方法

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

    file_dir = "C:/Users/lenovo/Desktop/xxxxxxxxx/xxxxxxxxxx"
    import os

    files = os.listdir(file_dir)

    Filelist = []

    for home, dirs, files in os.walk(file_dir):
        for filename in files:
            if '(' in filename:
                pass
                print(filename)
                os.remove(os.path.join(home, filename))
            else:
                pass

        for name in dirs:
            if " (" in name:
                print(name)
                os.rmdir(os.path.join(home, name))

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-07-31 16:35:50  更:2021-07-31 16:37:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/2 7:18:23-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码