python处理excle实例之字符串减法

本文最后更新于:2020年3月5日 晚上

python+excel

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
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/python
# -*- coding: utf-8 -*-
from openpyxl import load_workbook


# 求字符串A-B
def str_A_cut_B(a, b):
if b:
for x in a:
if x in b:
a = a.replace(x, "")
return a


# 写入模式打开output.txt
# f = open("output.txt", "w")

# 打开excel名为test
wb = load_workbook(filename='test.xlsx')

# 打开工作表Sheet1
sheet = wb['Sheet1']

for i in range(2, 36):
temp = 'C' + str(i)

# 将temp单元格的内容存入str1
str1 = sheet[temp].value
str0 = "张 朱 颜 赵 杨 施 胡 何 秦 刘 夏 鹏 李 唐 许 王 邓 顾"
z = str_A_cut_B(str0, str1)

# 若单元格为空,换行输出
if str1:
# 输出格式
for j in range(len(z)):
print(z[j], end=' ') # 不换行输出
# print(z[j], end=' ', file=f)
print('\n', end='')
# print('\n', end='', file=f)
else:
print('\n', end='')
# print('\n', end='', file=f)
# f.close()