Python读取/清洗/写入Excel

 
 
import pandas as pd
import re
import xlwt
a=pd.DataFrame(pd.read_excel(R'F:\001识别后\A.xlsx'))
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('My Worksheet')
k=1
for i,j in zip(a['A'],a['B']):
    try:
        if i[:4].isdigit():
            sch_code=i[:4]
            schname=re.findall('([\\u4e00-\\u9fa5]{0,})\[*',i[4:])
            num=re.findall(r'\d+',j)
            num0=num[0]
            sch_name = schname[0].strip('., •■')
            worksheet.write(k, 0, i)
            worksheet.write(k, 1, j)
            worksheet.write(k, 2, sch_code)
            worksheet.write(k, 3, sch_name)
            worksheet.write(k, 8, num0)
            k = k + 1
        elif i[:1].isalpha():
            major_id=i[:2]
            major_name=re.findall('(\D+)\(|(\D+)',i[2:])[0]
            other = re.findall('\((\D+)\)', i)
            if j[:1].isdigit():
                num1=re.findall('\d+',str(j))
                num10=num1[0]#学费
                num11=num1[1]#年制
                num12=num1[2]#人数
            else:
                num1 = re.findall('\d+', str(j))
                num10 = '待定'  # 学费
                num11 = num1[0]  # 年制
                num12 = num1[1]  # 人数
            # all = re.findall('\D+', i[2:])
            worksheet.write(k, 0, i)
            worksheet.write(k, 1, str(j))
            worksheet.write(k, 2, sch_code)
            worksheet.write(k, 3, sch_name)
            worksheet.write(k, 4, major_id)
            worksheet.write(k, 5, major_name)
            worksheet.write(k, 6, num10)
            worksheet.write(k, 7, num11)
            worksheet.write(k, 8, num12)
            if other!=[]:
                worksheet.write(k, 9, other[0])
            k=k+1
        else:
            print(k+1)
            worksheet.write(k, 0, i)
            worksheet.write(k, 1, j)
            k = k + 1
    except:
        print(k+1)
        worksheet.write(k, 0, i)
        worksheet.write(k, 1, j)
        k = k + 1
workbook.save(R'F:\002跑程序后\B.xls')

猜你喜欢

转载自blog.csdn.net/weixin_42080280/article/details/80589396