感叹一下,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/
|