字符串的转义问题

字符串中的转义问题

1
2
c = 'C:\windows\nt'
c = 'C:\\windows\\nt'

更简单的方式,r前缀将字符串不要转义了

1
c = r'C:\windows\nt'

前缀r,把里面的所有字符当普通字符对待,则转义字符就不用转义了

1
2
3
4
5
a = 'abc'
b = 123
c = f'{a}********{b}'
c
'abc********123'

这里说明一下f 是用来构造字符串的。

布尔值

False等价布尔值,相当于bool(value)

1
2
3
test = [] 
bool(test)
# 返回False

字符串的类型比较

1
2
type(1) == int # 注意不能是'int' 这样'int'表示为字符串
# 返回True
1
2
3
4
5
type(list), type(str) ,type(type)
(type,type,type) # type # type 是元类

type(True) == bool
True
1
2
3
4
5
6
7
8
isinstance(1, int)
True
isinstance(1, str)
False
isinstance(True, bool)
True
isinstance(1,(str, list, tuple, int)) ## 其中有一个符合返回True
True

input 输入

1
2
3
4
x = input('>>>') ## 阻塞输入,返回一个字符串
>>>
print(x)
123

print分隔符

1
2
print(1, 2, 3, [], [1,2,3],sep='\t\t')
print(1, 2, 3, sep='#',end='\t') # 间隔使用 # 最后使用\t

math 天花板地板

1
2
3
4
5
6
7
8
print (math. ceil (1.0), math.ceil (1.3), math.ceil (1.5), math.ceil (1.6), math.ceil (1.8))
1 2 2 2 2
print (math.floor (1.0), math. floor (1.3), math. floor (1.5), math. floor (1.8), math. floor (2.0))
1 1 1 1 2
math.ceil (-1.0), math.ceil (-1.3), math.ceil (-1.5), math.ceil (-1.6), math.ceil (-1.8)
(-1, -1, -1, -1, -1)
math.floor (-1.0), math. floor (-1.3), math. floor (-1.5), math. floor (-1.8), math. floor (-2. 9)
(-1, -2 ,-2 ,-2 ,-2 )

int()只截取整数部分
round() 找0.5最近的偶数,(四舍六入、0.5取偶)

数据库连接与建表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pymysql

# 创建数据库连接
conn = pymysql.connect(host='localhost', user='root', password='password', database='mydb')

# 创建游标对象
cursor = conn.cursor()

# 查询表是否存在
cursor.execute("SHOW TABLES LIKE 'k8s_memory_7d_use'")
result = cursor.fetchone()

# 如果表不存在,则创建表
if not result:
cursor.execute("""
CREATE TABLE k8s_memory_7d_use (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
k8s_cluster VARCHAR(50) NOT NULL,
namespace VARCHAR(50) NOT NULL,
pod VARCHAR(50) NOT NULL,
instance VARCHAR(50) NOT NULL,
memory_7d_arrive_now_max_unit_M INT(11) NOT NULL,
Memory_Limit_M INT(11) NOT NULL,
Memory_Use_percentage FLOAT(6,2) NOT NULL
)
""")
print("Table k8s_memory_7d_use created successfully")

# 关闭游标和连接
cursor.close()
conn.close()

评论