python 文件合并和编号
# -*- coding:utf-8 -*-
import os
import re
p1=r"([0-9][0-9][AB])\.\w{3}$"
pattern1=re.compile(p1)
def get_dir(zz):
listdir=[]
for filename in os.listdir('./%s'%zz):
listdir.append(filename)
# print(filename)
# print(type(filename))
# print(listdir)
return listdir def joint_b0(listdir,zz):
if not os.path.exists('./new/'):
os.makedirs('./new/')
fw=open('./new/%s.csv'%(zz),'a')
for i in listdir:
# print(i)
j=1000
matcher1=re.findall(pattern1,i)
fr=open('./%s/%s'%(zz,i))
for line in fr.readlines():
try:
# print(len(line))
if(len(line)==1):
continue
else:
fw.write(matcher1[0]+'_%s'%j+','+line.strip('\n')+'\n')
j=j+1
except:
pass
fr.close()
fw.close() def joint_b1(listdir,zz):#单独拼接
if not os.path.exists('./new/'):
os.makedirs('./new/')
fw=open('./new/%s.csv'%(zz),'a')
for i in listdir:
# print(i)
j=1
matcher1=re.findall(pattern1,i)
fr=open('./%s/%s'%(zz,i))
for line in fr.readlines():
fw.write(line) fr.close()
fw.close()
if __name__=="__main__": # dir_list=['b0'],'b3','b4','b2_idsd','b3_idcd' dir_list=['b4']#用于编号和拼接,会在new目录下生成编号号码的文件
for i in range(len(dir_list)):
listdir=get_dir(dir_list[i])
# print(listdir)
joint_b0(listdir,dir_list[i]) # dir_list=['stock']#用于拼接,将需要拼接的放到stock目录下,会在new目录下生成stock文件,然后根据需要修改名称
# for i in range(len(dir_list)):
# listdir=get_dir(dir_list[i])
# # print(listdir)
# joint_b1(listdir,dir_list[i])
python 文件合并和编号的更多相关文章
- Python之FTP多线程下载文件之分块多线程文件合并
Python之FTP多线程下载文件之分块多线程文件合并 欢迎大家阅读Python之FTP多线程下载系列之二:Python之FTP多线程下载文件之分块多线程文件合并,本系列的第一篇:Python之FTP ...
- Python文件操作---合并文本文件内容
目前一个用的比较多的功能:将多个小文件的内容合并在一个统一的文件中,对原始文件重命名标记其已被处理过.之前使用其他脚本写的,尝试用python写了一下,顺便熟悉一下python的文件处理命令. 原始文 ...
- python实现合并两个文件并打印输出
# python实现合并两个文件并打印输出 import fileinput file_Path1 = input("请输入第一个合并文件:") file_Path2 = inpu ...
- 【转】Python编程: 多个PDF文件合并以及网页上自动下载PDF文件
1. 多个PDF文件合并1.1 需求描述有时候,我们下载了多个PDF文件, 但希望能把它们合并成一个PDF文件.例如:你下载的数个PDF文件资料或者电子发票,你可以使用python程序合并成一个PDF ...
- python pandas合并多个excel(xls和xlsx)文件(弹窗选择文件夹和保存文件)
# python pandas合并多个excel(xls和xlsx)文件(弹窗选择文件夹和保存文件) import tkinter as tk from tkinter import filedial ...
- 利用Python将多个PDF文件合并
from PyPDF2 import PdfFileMerger import os files = os.listdir()#列出目录中的所有文件 merger = PdfFileMerger() ...
- Python实现多个pdf文件合并
背景 由于工作原因,经常需要将多个pdf文件合并后打印,有时候上网找免费合并工具比较麻烦(公司内网不能访问公网),于是决定搞个小工具. 具体实现 需要安装 PyPDF2 pip install PyP ...
- Python——使用高德API获取指定城指定类别POI并实现XLSX文件合并
# 以下内容为原创,转载请注明出处 1 import xlrd # 读xlsx import xlsxwriter # 写xlsx import urllib.request # url请求,Pyth ...
- 【Python文件处理】递归批处理文件夹子目录内所有txt数据
因为有个需求,需要处理文件夹内所有txt文件,将txt里面的数据筛选,重新存储. 虽然手工可以做,但想到了python一直主张的是自动化测试,就想试着写一个自动化处理数据的程序. 一.分析数据格式 需 ...
随机推荐
- android中配置文件property的用途以及使用<转>
1.首先在源代码根目录(src下)下创建一个名为netconfig.properties的文件(也可以在其他目录下). 2.打开netconfig.properties文件,在该文件中添加下列代码. ...
- UI5-文档-4.25-Sorting and Grouping
为了使我们的发票列表更加用户友好,我们将它按字母顺序排序,而不是仅仅显示来自数据模型的顺序.此外,我们还引入了组,并添加了发布产品的公司,以便更容易使用数据. Preview The list is ...
- Apache Hive 存储方式、压缩格式
简介: Apache hive 存储方式跟压缩格式! 1.Text File hive> create external table tab_textfile ( host string com ...
- Syncthing搭建
现在貌似只有windows和linux比较号装. 安装 先从官网下载好Windows 32位版(我本本对应的系统版本)的Syncthing,解压后可以看到如下文件结构 Syncthing文件结构 ...
- Fiddler 捕获 nodejs 模拟的http请求
1.设置Fiddler Tools->Options-> Connections Allow remote computers to connect: 2.nodejs 请求有多种 2.1 ...
- Java 深浅拷贝
2016-07-02 1深拷贝:不仅拷贝对象,而且对象所引用地址的内容一块拷贝.改变一个对象的某个属性,并不影响另一个对象所引用的内容. 2浅拷贝:仅拷贝对象本身,并不对所引用(所指的)内容进行拷贝, ...
- SQL某时间段记录查询
--查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: select * from info wh ...
- [leetcode]367. Valid Perfect Square验证完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- php的静态化
原理,将动态的页面,存储为静态的HTML静态页,使浏览器直接请求该静态页. 测试:一个PHP动态页面与一个静态页面所消耗的时间 一般可以使用apache自带的ab(apache bench)程序来测试 ...
- phpcms如何给已有的模块添加新功能?
phpcms如何给已有的模块添加新功能? 方法一:直接在模块里的控制器文件中添加功能. 不建议使用此方法,因为一旦phpcms升级,有可能会覆盖模块中的文件, 导致你添加的功能丢失. 方法二:新建一个 ...