为了账号安全,请及时绑定邮箱和手机立即绑定

Python读取符合一定条件的Excel数据,复制到文本文件中

Python读取符合一定条件的Excel数据,复制到文本文件中

潇潇雨雨 2018-06-28 06:47:03
大神们,求助,求助Python初学者,尝试利用Python遍历读取Excel中B列的数据,并以其数据对应为文本文件名称,同时,复制Excel中A列的数值到对应的文本文件
查看完整描述

2 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

用openpyxl这是一个例子:
 import openpyxl
import re

def Exceldivide(file_dir):
 wb=openpyxl.load_workbook(file_dir)         #打开原有的excel表
 sheet=wb.get_sheet_by_name('Sheet1')
 tuple(sheet['A1':'C3'])

 wb.create_sheet('Sheet2')                 #新建一个表
 sheet2=wb.get_sheet_by_name('Sheet2')
 tuple(sheet2['A1':'C3'])

 L1=re.compile(r'\d\d/\d\d/\d\d\d\d')      #日期格式
 L2=re.compile(r'[a-zA-Z0-9_]+@[a-zA-Z0-9-]+.com')   #邮件格式
 l1=[]
 l2=[]
 for rows in sheet['A1':'C3']:           #提取日期和邮件数据
     for cell in rows:
         A=L1.search(cell.value)
         a=A.group()
         B=L2.search(cell.value)
         b=B.group()
 for rows in sheet2['A1':'A9']:         #把日期数据写入新表
    for cell in rows:
        cell.value=a
        print(cell.coordinate,cell.value)
 for rows in sheet2['B1':'B9']:        #把邮件数据写入新表      
    for cell in rows:
        cell.value=b
        print(cell.coordinate,cell.value)
 return wb

g=Exceldivide('C:\\Users\\Desktop\\111_copy.xlsx')
g.save('C:\\Users\\Desktop\\111_copy.xlsx')    #保存

你只要输出修改为txt就好。

查看完整回答
反对 回复 2018-07-14
?
Helenr

TA贡献1780条经验 获得超3个赞

不需要库,只需要将文件保存问csv

with open('xxx.csv')as f:
    for line in f.readlines():
        line=line.split(',')
        with open('..//'+line[1]+'.csv') as fi:
            fi.write(line[0])

搞定

查看完整回答
反对 回复 2018-07-14
  • 2 回答
  • 0 关注
  • 1762 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信