python应用简单总结

跨文件调方法

1
2
3
# FileA.py
def test():
print("testA")
1
2
3
# FileB.py
import FileA
FileA.test()

跨文件调类

1
2
3
4
5
# FileA.py
class A {
def test(self):
print("testA")
}
1
2
3
4
# FileB.py
from FileA import A
a = A()
a.test()

随机掉用list内元素

1
2
3
import random
print(random.choice([1, 2, 3]))
# 输出1 2 3中随机一个

字符串模板化

1
2
3
4
5
6
7
8
from string import Template
templateStr="hello, $name"
print(Template(templateStr).substitute(name="world"))
# hello, world

data = "hello, {name}"
print(data.format(name="world"))
# hello, world

根据字符串调用方法

1
2
3
4
5
6
7
8
# 利用方法名字符串调用方法
class Test:
def test(self, param: str):
print(param)

def main():
func = getattr(self, "test")
data = func()

更多参照:https://mozillazg.com/2016/03/python-exec-function-globals-and-locals-arguments.html

常用文件处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import os
import shutil

def createFile(content: str, folderPath: str, fileName: str):
# 路径不存在时创建文件夹
if not os.path.exists(folderPath):
os.makedirs(folderPath)
# 需要设置newline="\n", encoding="utf-8"时追加参数
writeContent(folderPath + fileName, content)

def copyFile(fromPath: str, toPath: str):
shutil.copy(fromPath, toPath)

def getFileContent(filePath: str, newline="\n", encoding="utf-8") -> str:
file = open(filePath,'r', encoding=encoding, newline=newline)
data = file.read()
file.close()
return data

def writeContent(filePath: str, content: str, newline="\n", encoding="utf-8"):
# newline控制换行符,Windows下设置为\r\n,Unix下设置为\n
file = open(filePath, "w+", encoding=encoding, newline=newline)
file.write(content)
file.close()

生成随机日期

1
2
3
4
5
6
7
8
9
10
11
12
# 生成日期范围内的日期
from datetime import datetime, timedelta

def random_date(startDate, endDate) -> str:
delta = endDate - startDate
seconds = delta.total_seconds()
n = random.randrange(seconds)
return startDate + timedelta(seconds=n)

startDate = datetime.strptime('1/1/2022', '%m/%d/%Y')
endDate = datetime.strptime('12/31/2022', '%m/%d/%Y')
print(str(random_date(startDate, endDate)))

Excel数据转list

Excel数据直接粘贴到编辑器中时,单元格数据之间有换行符\t,可以借此拆分

1
2
3
4
5
6
7
8
9
10
11
12
data = '''
test1 test2 test3
test11 test22 test33
'''
data = data.splitlines()
data.remove('')
result = []
for tmp in data:
tmpList = tmp.split("\t")
result.append(tmpList)
print(result)
# [['test1', 'test2', 'test3'], ['test11', 'test22', 'test33']]
作者

Etsu

发布于

2023-02-14

更新于

2023-02-14

许可协议

评论