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一直主张的是自动化测试,就想试着写一个自动化处理数据的程序. 一.分析数据格式 需 ...
随机推荐
- linux no space left on device
一般有两个原因: 1.磁盘空间不足 2.inode不足 用df -h查看磁盘空间使用情况 用df -i查看inode使用情况
- delphi实现两个目录路径的链接
filepath := PathJoin(['C:', 'path1', 'path2\', 'a.doc']); // filepath = 'C:\path1\path2\a.doc' 代码: f ...
- Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:generate (default-cli) on project : <properties> resource does not exist
使用mybatis-generator自动生成mapper.dao等文件时,报错如下: org.apache.maven.lifecycle.LifecycleExecutionException: ...
- cdoj第13th校赛初赛F - Fabricate equation
http://acm.uestc.edu.cn/#/contest/show/54 F - Fabricate equation Time Limit: 3000/1000MS (Java/Other ...
- 134. Gas Station (Array; DP)
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- CentOS 安装 Docker CE
准备工作 系统要求 Docker CE 支持 64 位版本 CentOS 7,并且要求内核版本不低于 3.10. CentOS 7 满足最低内核的要求,但由于内核版本比较低,部分功能(如 overla ...
- Partition List双色问题链表版
[抄题]: Given a linked list and a value x, partition it such that all nodes less than x come before no ...
- ADF学习实用网站
ADF中所有组件工功能例子 http://jdevadf.oracle.com/adf-richclient-demo/faces/components/dialog.jspx;jsessionid= ...
- 一句话引发的思考 - synchronized/super
https://blog.csdn.net/cool__wang/article/details/52459380#commentBox
- Linux readelf命令
一.简介 readelf用来显示一个或者多个elf格式的目标文件的信息,可以通过它的选项来控制显示哪些信息.这里的elf-file(s)就表示那些被检查的文件.可以支持32位,64位的elf格式文件, ...