joint python文件拼接
# -*- coding:utf-8 -*-
import os
import re
p1=r"([0-9][0-9][AB])\.\w{3}$" p2=r"^.+\,(\d{4}).+" pattern1=re.compile(p1)
pattern2=re.compile(p2)
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=0
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
if(j==0):#跳过第一行
# print("xixi") j=j+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(): # print(len(line))
if(len(line)==4):
continue
else:
fw.write(line)
# pass fr.close()
fw.close() def sort_joint():
# dir_list=['b0'],'b3','b4','b2_idsd','b3_idcd' dir_list=['b1']#用于编号和拼接,会在new目录下生成编号号码的文件
for i in range(len(dir_list)):
listdir=get_dir(dir_list[i])
# print(listdir)
joint_b0(listdir,dir_list[i]) def joint_only():
dir_list=['stock2']#用于拼接,将需要拼接的放到stock目录下,会在new目录下生成stock文件,然后根据需要修改名称
for i in range(len(dir_list)):
listdir=get_dir(dir_list[i])
# print(listdir)
joint_b1(listdir,dir_list[i]) def updata(zz):
fr=open('./new/%s'%(zz))
fw=open('./new/new.csv','a')
for line in fr.readlines():
matcher2=re.findall(pattern2,line)
if(matcher2):
fw.write(matcher2[0]+','+line) if __name__=="__main__":
sort_joint()
# joint_only()
# updata('b4.csv')
joint python文件拼接的更多相关文章
- Python 文件拼接
# -*- coding:utf-8 -*- import re import csv file = open('make_setup.cfg', 'w+') with open("tyb. ...
- Python文件IO
Python文件IO 有如下文本内容,文件路径为D:\temp,文件名称为lyric.txt, line1 Look ! line2 If U had one shot line3 One oppor ...
- 文件参数化-utp框架之根据yaml文件自动生成python文件+utp运行用例
根据yaml文件自动生成python文件 utp框架: bin目录:存放执行文件(run.py) cases目录:存放生成的用例的python文件(该目录下的文件为根据data目录下的测试用例生成的p ...
- 第十二章 Python文件操作【转】
12.1 open() open()函数作用是打开文件,返回一个文件对象. 用法格式:open(name[, mode[, buffering[,encoding]]]) -> file obj ...
- Linux下Python 文件内容替换脚本
Linux下Python 文件替换脚本 import sys,os if len(sys.argv)<=4: old_text,new_text = sys.argv[1],sys.argv[2 ...
- 【Python文件处理】递归批处理文件夹子目录内所有txt数据
因为有个需求,需要处理文件夹内所有txt文件,将txt里面的数据筛选,重新存储. 虽然手工可以做,但想到了python一直主张的是自动化测试,就想试着写一个自动化处理数据的程序. 一.分析数据格式 需 ...
- Python文件使用“wb”方式打开,写入内容
Python文件使用"wb"方式打开,写入字符串会报错,因为这种打开方式为:以二进制格式打开一个文件只用于写入.如果该文件已存在则将其覆盖.如果该文件不存在,创建新文件. 所以写入 ...
- Python 文件操作函数
这个博客是 Building powerful image classification models using very little data 的前期准备,用于把图片数据按照教程指示放到规定的文 ...
- python文件I/O(转)
Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...
随机推荐
- 17 网络编程 C/S架构介绍
1.什么是C/S架构 C指的是client(客户端软件),S指的是Server(服务器软件),本章的重点是教大家写一个C/S架构的软件,实现服务端软件与客户端软件基于网络通信. 2.计算机基础的知识- ...
- python的线上环境配置
1.安装python 2.7 http://www.cnblogs.com/strikebone/p/3970512.html 2.安装相关前置工具 pip, Django http://www ...
- inotify用法简介及结合rsync实现主机间的文件实时同步
一.inotify简介 inotify是Linux内核2.6.13 (June 18, 2005)版本新增的一个子系统(API),它提供了一种监控文件系统(基于inode的)事件的机制,可以监控文件系 ...
- 【340】GIS related knowledge
mpk: 对地图文档以及所有引用的数据源进行打包可创建经过压缩的单个 .mpk 文件.参考,可以通过 Package Map 工具实现. Collector for ArcGIS Survey123 ...
- Error: Cannot find a valid baseurl for repo: epel
修改一下/etc/yum.repos.d/epel.repo文件, enable=1改为enable=0
- python的多线程和守护线程
1.创建一个多线程 import threading import time ''' def threading_func(num): print("running on number:%s ...
- 在Objc项目中调用Swift
之前的文字中记录了在Swift项目中调用OC的相关代码,比较简单直接 传送门 但是在OC中调用swift代码则不是那么的和谐,网络上很多文章业已经有点陈旧.记录步骤如下: 1.创建OC项目 (1)启动 ...
- swift - 代码创建 pickerView 显示或隐藏横线
import UIKit class VC1: UIViewController { fileprivate lazy var pickerV : UIPickerView = { let v = U ...
- 7-找了一上午的BUG
#include <iostream>#include <cstring>#include <algorithm>#define MAX 1<<28;u ...
- silverlight的Datagrid控件列绑定属性笔记
<data:DataGridTemplateColumn Header="给作者留言"> <data:DataGridTemplateColumn.CellTem ...