samTools-python | Python的知识 | 字符和字节的转换
·
samTools
s = [[],[],[],'4','[]','不',[],4,'sha']
class get_str_inlist():
# def __init__(self,s,getall=False):
# self.s = s
# self.getall = getall
# print(self.getall,'self.getall++')
#"""判断一个unicode是否是汉字"""
def is_chinese(self,uchar):
if u'\u4e00' <= uchar <= u'\u9fff':
return True
else:
return False
#"""判断一个unicode是否是数字"""
def is_number(self,uchar):
if u'\u0030' <= uchar <= u'\u0039':
return True
else:
return False
#"""判断一个unicode是否是英文字母"""
def is_alphabet(self,uchar):
if (u'\u0041' <= uchar <= u'\u005a') or (u'\u0061' <= uchar <= u'\u007a'):
return True
else:
return False
#"""判断是否是(汉字,数字和英文字符之外的)其他字符"""
def is_have_meaning(self,uchar):
if (self.is_chinese(uchar) or self.is_number(uchar) or self.is_alphabet(uchar)):
return True
else:
return False
def get_str_inlist_fan(self,s,getall=False):
longtext = ''
for i in s:
i = str(i)
for ii in i:
if self.is_chinese(ii):
if getall:
longtext += i.strip()
break
else:
return i
if self.is_number(ii):
if getall:
longtext += i.strip()
break
else:
return i
if self.is_alphabet(ii):
if getall:
longtext += i.strip()
break
else:
return i
return longtext
# getall为False默认只返回第一个匹配到的有意义的元素,为 True则会将所有有意义的元素进行字符串拼接放回拼接后的字符串
get_str_inlist_result = get_str_inlist().get_str_inlist_fan(s,getall=False)
print(get_str_inlist_result,'get_str_inlist_result++')
# -----------------------------根据坐标来作图----------------------------------------------
zuobiao = '''
[
{ x: 0, y: 6 }, { x: 3, y: 6 }, { x: 6, y: 6 }, { x: 9, y: 6 },
{ x: 14, y: 6 }, { x: 19, y: 6 }, { x: 21, y: 6 }, { x: 23, y: 6 },
{ x: 27, y: 6 }, { x: 29, y: 6 }, { x: 31, y: 6 }, { x: 34, y: 6 },
{ x: 36, y: 6 }, { x: 40, y: 6 }, { x: 42, y: 6 }, { x: 46, y: 6 },
{ x: 49, y: 6 }, { x: 51, y: 6 }, { x: 53, y: 6 }, { x: 58, y: 6 },
{ x: 60, y: 6 }, { x: 64, y: 6 }, { x: 67, y: 6 }, { x: 72, y: 6 },
{ x: 74, y: 6 }, { x: 78, y: 6 }, { x: 81, y: 6 }, { x: 83, y: 6 },
{ x: 87, y: 6 }, { x: 90, y: 6 }, { x: 93, y: 6 }, { x: 97, y: 6 },
{ x: 102, y: 6 }, { x: 105, y: 6 }, { x: 107, y: 6 }, { x: 109, y: 6 },
{ x: 112, y: 6 }, { x: 117, y: 6 }, { x: 121, y: 6 }, { x: 124, y: 7 },
{ x: 129, y: 7 }, { x: 131, y: 7 }, { x: 133, y: 7 }, { x: 137, y: 7 },
{ x: 139, y: 7 }, { x: 144, y: 7 }, { x: 148, y: 7 }, { x: 150, y: 7 },
{ x: 155, y: 7 }, { x: 160, y: 7 }, { x: 163, y: 7 }, { x: 166, y: 7 },
{ x: 169, y: 7 }, { x: 172, y: 7 }, { x: 176, y: 7 }, { x: 180, y: 7 },
{ x: 183, y: 7 }, { x: 187, y: 7 }, { x: 192, y: 7 }, { x: 197, y: 6 },
{ x: 199, y: 6 }, { x: 203, y: 6 }, { x: 206, y: 6 }, { x: 210, y: 6 },
{ x: 213, y: 6 }, { x: 216, y: 6 }, { x: 219, y: 6 }, { x: 222, y: 6 },
{ x: 227, y: 6 }, { x: 229, y: 6 }, { x: 231, y: 6 }, { x: 233, y: 6 },
{ x: 235, y: 6 }, { x: 240, y: 6 }, { x: 243, y: 6 }, { x: 245, y: 6 },
{ x: 247, y: 6 }, { x: 252, y: 6 }, { x: 254, y: 6 }, { x: 256, y: 6 },
{ x: 259, y: 6 }, { x: 262, y: 6 }, { x: 267, y: 6 }, { x: 271, y: 6 }
]
'''
x_list = []
y_list = []
x = re.findall('x: (\d+),',zuobiao)
for i in x:
x_list.append(int(i))
y = re.findall('y: (\d+) }',zuobiao)
for i in y:
y_list.append(int(i))
print(x_list)
print(y_list)
plt.plot(x_list, y_list, 'r-o')
# plt.plot(x, y)
plt.show()
# --------------------------我的日期时间工具---------------------
# -*- coding: UTF-8 -*-
import re
import time
import datetime
import calendar
import random
# 日期处理
# 第一步将字符串日期转datetime格式->strptime方法
today_datetime = datetime.strptime('20211202', "%Y%m%d")
# 第二步datetime的加减,对日期和时间进行加减实际上就是把datetime往后或往前计算,得到新的datetime。加减可以直接用+和-运算符,不过需要导入timedelta这个类
lastdate = today_datetime - timedelta(days=1)
# 第三步将datetime转成str输出->使用strftime方法
lastdate_str = lastdate.strftime("%Y-%m-%d %H:%M:%S")
# 获得当前日期的周一字符串
from datetime import datetime, timedelta
this_monday_str = datetime.strftime(datetime.now() - timedelta(datetime.now().weekday()), "%Y-%m-%d")
# 可以返回当前是星期几
print(datetime.datetime.now().weekday(),'datetime.datetime.now().weekday()++') # >>> 1
# 现在的年月日时分秒
print(datetime.datetime.now(),'datetime.datetime.now()++') # >>> 2021-06-08 21:58:51.003655
# 计算两个日期相隔多少天
d = datetime.date(2021,6,8)
d1 = datetime.date(2021,6,4)
print(d.__sub__(d1)) # >>> 4 days, 0:00:00
print(d.__sub__(d1).days) # >>> 4
# 也可以利用datatime构造日期对象进行相减
dt = datetime.datetime(2021,6,12,23,59,59)
dt1 = datetime.datetime(2021,5,12,23,59,59)
print(dt.__sub__(dt1).days) # >>> 31
# 时间的加减
dt = datetime.datetime(2021,6,12,23,59,59)
# 昨天
print(dt + datetime.timedelta(days=-1)) # >>> 2021-06-11 23:59:59
# 明天
print(dt + datetime.timedelta(days=1)) # >>> 2021-06-13 23:59:59
# 下一个小时
print(dt + datetime.timedelta(hours=1)) # >>> 2021-06-13 00:59:59
# 上一秒
print(dt + datetime.timedelta(seconds=-1)) # >>> 2021-06-12 23:59:58
# 时间输出的格式化 %Y-%m-%d %H:%M:%S%y-%m-%d %H:%M:%S
print(dt.strftime("%Y-%m-%d")) # >>> 2021-06-12
# 时间戳转换为日期
t=1614126918.0
print(datetime.datetime.fromtimestamp(t)) # >>> 2021-02-24 08:35:18
# 当前日期是本年的第几周,周几
year = 2021
month = 6
day = 13
year,week,day = datetime.date(year, month, day).isocalendar()
print(year,week,day,'year,week,day++') # >>> 2021 23 7
# 当前日期是本年的第几天
t1 = datetime.datetime.now()#获取当前时间
t2 = datetime.datetime(2021,1,1)#获取指定时间
t3 = t1 - t2
print(t3.days) # >>> 158
多python的管理问题

# 关于路径
# 返回当前文件的路径
>>> __file__
'C:/Users/EDZ/Desktop/sqy_to_git/business_cloud/spider_data_deal/sqy_test.py'
# 返回window格式的绝对路径
>>> os.path.abspath(__file__)
'C:\\Users\\EDZ\\Desktop\\sqy_to_git\\business_cloud\\spider_data_deal\\sqy_test.py'
# 返回上级目录的路径
>>> os.path.dirname(os.path.abspath(__file__))
'C:\\Users\\EDZ\\Desktop\\sqy_to_git\\business_cloud\\spider_data_deal'
# 判断是否存在某个路径
>>> os.path.exists('C:\\Users\\EDZ\\Desktop\\sqy_to_git\\business_cloud\\spider_data_deal')
True
# 进行md5
import hashlib
timestamp = str(time.time())
md5_t = hashlib.md5()
md5_t.update(timestamp.encode('utf-8'))
md5_result = md5_t.hexdigest()
# 文件保存
import openpyxl
import os
import time
import datetime
datetime_now = datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d%H%M%S")
file_save = './{}/{}_{}.xlsx'.format('test_easydl','EasyDL',datetime_now)
if os.path.exists(file_save):
wb = openpyxl.Workbook(file_save)
wb.save(file_save)
wb = openpyxl.load_workbook(file_save)
ws = wb.active
import json
# 字符转字节
data = {"test":"t1"}
tt = bytes(json.dumps(data), encoding="utf8")
for t in tt:
print(t)
# 字节转字符
tt2 = bytes([123,34,116,101,115,116,34,58,32,34,116,49,34,125])
tt3 = []
print(tt2)
for t in tt2:
tt3.append(t)
print(tt3)
# python和java字节码互换,主要的原因是java是有符号数,python的无符号数
def pb2jb(byte_arr):
"""
python字节码转java字节码
:param byte_arr:
:return:
"""
return [int(i) - 256 if int(i) > 127 else int(i) for i in byte_arr]
def jb2pb(byte_arr):
"""
java 字节码转python字节码
:return:
"""
return [i + 256 if i < 0 else i for i in byte_arr]
# 字节数组转成hex打印
def print_hex(bytes):
l = [hex(int(i)) for i in bytes]
print(" ".join(l))
更多推荐



所有评论(0)