简单代码片段
2的n次方,猜数字,压缩文件并输出,简单爬标题
import random
import sys
import os
import time
import zipfile
import requests,json
#2的n次方,10以内
while i in range(1,10):
print(2<<i)
i+=1
#猜数字
while True:
s = input('Enter something : ')
if len(s)<3:
print("nss")
continue
print("nono")
print("good")
if s == 'quit':
break
print('Length of the string is', len(s))
print('Done')
while 1>0:
g = int(input('guess :'))
d = random.randint(1 , 100)
if g<d:
print("low")
elif g>d:
print("high")
else:
print("done")
break
print("good")
while True:
s = input("enter:")
if s=="quit":
break
if len(s)<3:
print("too small")
continue
print("good")
#压缩文件并输出保存
source = ['d:\\888\\','d:\\7798\\']
target_dir = ['d:\\888back']
'''
target = target_dir+os.sep+ \
time.strftime('%Y%m%d')+'.zip'
if not os.path.exists(target_dir):
os.mkdir(target_dir)
zip_command= 'zip -r {0} {1}'.format(target,' '.join(source))
print('zip command is: \n',zip_command)
print('running:')
if os.system(zip_command)==0:
print('ok')
else:
print('error')
'''
def zip_files(files,zip_name):
zip= zipfile.ZipFile(zip_name,'w',zipfile.ZIP_DEFLATED)
for file in files:
print('compressing',file)
zip.write(file)
zip.close()
print('ok')
files=[]
for i in os.listdir('d:\\888\\'):
i=source[0]+i
files.append(i)
print(files)
#files=['d:\\888\a.docx','d:\\888\b.xlsx']
zip_file='d:\\888back\\123.zip'
zip_files(files,zip_file)
#简单爬标题
if __name__ == '__main__':
target = 'https://bh.sb/post/category/main/'
requests.packages.urllib3.disable_warnings()
req = requests.get(url=target,verify=False)
def content(html):
# 内容分割的标签
str = '<article class="excerpt excerpt-one">'
content = html.partition(str)[2]
str1 = '<aside class="sidebar">'
content = content.partition(str1)[0]
return content # 得到网页的内容
str0=content(str(req.content,encoding='UTF-8'))
#print(str0)
def cut1(con):
title_list=[]
i=0
beg=0
num1=0
while i<len(con):
if num1>=0:
num1=con.find('title="[博海拾贝',beg)
else:
break
num2=con.find(' - 博海拾贝" target',num1)
if num1>=0:
title_list.append(con[num1+17:num2])
beg=num2
i+=100
return title_list
#str1=cut1(str0)
print(cut1(str0))
#print('asdf=adm'.split('a')[-2])
#str1='aa-bb-cc'
#print(str1.partition('-'))
##print(str(req.content,encoding='UTF-8').partition('<excerpt excerpt-one">'))
获得文本拼音首字母
from xpinyin import Pinyin
import os
import sys
#获得文本拼音首字母
f = open("d:/1.txt","r")
txt =f.readlines()
print(txt)
f.close()
f = open("d:/1.txt","r")
line = f.readline()
line = line[:-1]
print(line)
while line:
line =f.readline()
line = line[:-1]
print(line)
f.close()
data=[]
for lines in open("d:/1.txt","r"):
data.append(lines[:-1])
print(data)
p=Pinyin()
for i in data:
print(Pinyin().get_pinyin(i).title())
转移输出(这个我也没看懂)
#spam 123移到了 back here 后面
g,h,j=1,2,30
print(g,h,j)
temp = sys.stdout
sys.stdout = open('log.txt','w')
print('spam')
print(1,2,3)
sys.stdout.close()
sys.stdout = temp
print('back here')
print(open('log.txt').read())
pi的计算,ps脚本
import sys
import math
#print(math.pi)
#from math import pi
#print(pi)
#pi的计算
def main(argv):
# if len(argv) != 1 :
# sys.exit('Usage: calc_pi.py <n>')
print('\nComputing Pi v.01\n')
a = 1.0
b = 1.0 / math.sqrt(2)
t = 1.0 / 4.0
p = 1.0
for i in range(1,9):
at = (a + b) / 2
bt = math.sqrt(a * b)
tt = t - p * (a - at) ** 2
pt = 2 * p
a = at; b = bt; t = tt; p = pt
my_pi = (a + b) ** 2 / (4 * t)
accuracy = 100 * (math.pi - my_pi) / my_pi
print("Pi is approximately: " + str(my_pi))
print("Accuracy with math.pi: " + str(accuracy))
main(sys.argv[1:9])
#ps脚本
import os
import subprocess as sp
from glob import glob
class PowerShell:
# from scapy
def __init__(self, coding, ):
cmd = [self._where('PowerShell.exe'),
"-NoLogo", "-NonInteractive", # Do not print headers
"-Command", "-"] # Listen commands from stdin
startupinfo = sp.STARTUPINFO()
startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
self.popen = sp.Popen(cmd, stdout=sp.PIPE, stdin=sp.PIPE, stderr=sp.STDOUT, startupinfo=startupinfo)
self.coding = coding
def __enter__(self):
return self
def __exit__(self, a, b, c):
self.popen.kill()
def run(self, cmd, timeout=15):
b_cmd = cmd.encode(encoding=self.coding)
try:
b_outs, errs = self.popen.communicate(b_cmd, timeout=timeout)
except sp.TimeoutExpired:
self.popen.kill()
b_outs, errs = self.popen.communicate()
outs = b_outs.decode(encoding=self.coding)
return outs, errs
@staticmethod
def _where(filename, dirs=None, env="PATH"):
"""Find file in current dir, in deep_lookup cache or in system path"""
if dirs is None:
dirs = []
if not isinstance(dirs, list):
dirs = [dirs]
if glob(filename):
return filename
paths = [os.curdir] + os.environ[env].split(os.path.pathsep) + dirs
try:
return next(os.path.normpath(match)
for path in paths
for match in glob(os.path.join(path, filename))
if match)
except (StopIteration, RuntimeError):
raise IOError("File not found: %s" % filename)
if __name__ == '__main__':
# Example:
def run(a):
with PowerShell('GBK') as ps:
outs, errs = ps.run(a)
print('error:', os.linesep, errs)
print('output:', os.linesep, outs)
run('get-process powershell')
简单算法
1.计算某字串重复字符及数量及位置
def choselongstr(a):
x=list(str(a))
y=[]
z=[]
for i in range(0,len(x)):
if i==0:
y.append([x[i],1,[i+1]])
z.append(x[i])
if i>0:
for j in range(0,len(y)):
if x[i]==str(y[j][0]):
y[j][1]+=1
y[j][2].append(i+1)
elif x[i]!=str(y[j][0]) and j==(len(y)-1):
if x[i] not in z:
y.append([x[i],1,[i+1]])
z.append(x[i])
print(y)
choselongstr(522311276522)
#######################################
#[['5', 2, [1, 10]], ['2', 5, [2, 3, 7, 11, 12]], ['3', 1, [4]], ['1', 2, [5, 6]], ['7', 1, [8]], ['6', 1, [9]]]
2.人民币大写金额转小写(千亿以内金额)
以下代码经过优化,可以有效避免类似于7*0.1这种浮点运算的精度问题。
方法是在计算前字符化,再调用decimal.Decimal()
print(7*0.1)
#0.7000000000000001
#类似的还有 print(decimal.Decimal(123456.123))
#123456.123000000006868503987789154052734375
import decimal
def bigtosmall(amount):
chinese_num = {'零': 0, '壹': 1, '贰': 2, '叁': 3, '肆': 4, '伍': 5, '陆': 6, '柒': 7, '捌': 8, '玖': 9}
chinese_amount = {'分': 0.01, '角': 0.1, '元': 1, '拾': 10, '佰': 100, '仟': 1000, '圆': 1}
amount_float = 0
if '亿' in amount:
yi = re.match(r'(.+)亿.*', amount).group(1)
amount_yi = 0
for i in chinese_amount:
if i in yi:
amount_yi += chinese_num[yi[yi.index(i) - 1]] * chinese_amount[i]
if yi[-1] in chinese_num.keys():
amount_yi += chinese_num[yi[-1]]
amount_float += amount_yi * 100000000
amount = re.sub(r'.+亿', '', amount, count=1)
if '万' in amount:
wan = re.match(r'(.+)万.*', amount).group(1)
amount_wan = 0
for i in chinese_amount:
if i in wan:
amount_wan += chinese_num[wan[wan.index(i) - 1]] * chinese_amount[i]
if wan[-1] in chinese_num.keys():
amount_wan += chinese_num[wan[-1]]
amount_float += amount_wan * 10000
amount = re.sub(r'.+万', '', amount, count=1)
amount_yuan = 0
for i in chinese_amount:
if i in amount:
if amount[amount.index(i) - 1] in chinese_num.keys():
#print(amount[amount.index(i) - 1],chinese_num[amount[amount.index(i) - 1]] * chinese_amount[i])
amount_yuan += decimal.Decimal(str(chinese_num[amount[amount.index(i) - 1]])) * decimal.Decimal(str(chinese_amount[i]))
#print(amount_yuan)
amount_float += amount_yuan
return amount_float
文件重命名
# import os
#
# path=r'C:\Users\d\Desktop\pdf改名脚本\22\2022年\test\what'
#
# newname='new'
# oldname=[]
#
# def renamefile(path):
# num=0
# for dir,j,file in os.walk(path):
# for filename in file:
# oldname.append(file)
# new_name = os.path.join(dir, newname + '-' + str(num)) + os.path.splitext(os.path.join(dir)+str(filename))[1]
# old_name = os.path.join(dir, filename)
# os.rename(old_name, new_name)
# print(new_name)
# num+=1
# print(oldname)
# renamefile(path)
import os
import pandas as pd
def rename_files_with_mapping(root_folder_path):
# 定义旧文件名和新文件名的映射关系
mapping = {
'1.XLS': '材料出库单列表.XLS',
'2.XLS':'采购订单列表.XLS',
'3.XLS': '采购入库单列表.XLS',
'4.XLS':'产成品入库单列表.XLS',
'5.XLS': '调拨单.XLS',
'6.XLS':'调拨申请单.XLS',
'7.XLS': '发货单列表.XLS',
'8.XLS':'发票列表.XLS',
'9.XLS': '客户档案.XLS',
'10.XLS':'库龄分析.XLS',
'11.XLS': '库龄分析汇总表.XLS',
'12.XLS':'其他出库单列表.XLS',
'13.XLS': '其他入库单列表.XLS',
'14.XLS':'请购单列表.XLS',
'15.XLS': '销售出库单列表.XLS',
'16.XLS':'销售调拨单.XLS',
'17.XLS': '销售调拨单列表.XLS',
'18.XLS':'销售订单.XLS',
'19.XLS': '销售订单列表.XLS',
'20.XLS':'销售发货单.XLS',
'21.XLS': '销售发票列表.XLS',
'22.XLS':'销售现存量查询.XLS',
'23.XLS': '形态转换业务.XLS'
}
# 创建一个 DataFrame 来存储文件信息及其新的文件名
all_files_df = pd.DataFrame(columns=['folder', 'filename', 'new_filename'])
# 使用 os.walk 遍历根文件夹及其所有子文件夹
for foldername, _, filenames in os.walk(root_folder_path):
for filename in filenames:
# 检查文件是否在映射中
if filename in mapping:
# 构造新的文件名
new_filename = mapping[filename]
# 添加到 DataFrame
all_files_df = pd.concat([all_files_df, pd.DataFrame([{
'folder': foldername,
'filename': filename,
'new_filename': new_filename
}])], ignore_index=True)
# 遍历 DataFrame 并重命名文件
for _, row in all_files_df.iterrows():
old_file_path = os.path.join(row['folder'], row['filename'])
new_file_path = os.path.join(row['folder'], row['new_filename'])
# 确保新文件不存在,避免覆盖
if os.path.exists(old_file_path) and not os.path.exists(new_file_path):
os.rename(old_file_path, new_file_path)
print(f'重命名文件: {old_file_path} -> {new_file_path}')
# 替换 'your_root_folder_path' 为你要处理的根文件夹路径
root_folder_path = r'C:\Users\dsn059\Desktop\erp'
rename_files_with_mapping(root_folder_path)