joint python文件拼接

 1 # -*- coding:utf-8 -*-
 2 import os
 3 import re
 4 p1=r"([0-9][0-9][AB])\.\w{3}$"
 5
 6 p2=r"^.+\,(\d{4}).+"
 7
 8 pattern1=re.compile(p1)
 9 pattern2=re.compile(p2)
10 def get_dir(zz):
11     listdir=[]
12     for filename in os.listdir(‘./%s‘%zz):
13         listdir.append(filename)
14         # print(filename)
15         # print(type(filename))
16     # print(listdir)
17     return listdir
18
19 def joint_b0(listdir,zz):
20     if not os.path.exists(‘./new/‘):
21         os.makedirs(‘./new/‘)
22     fw=open(‘./new/%s.csv‘%(zz),‘a‘)
23     for i in listdir:
24         # print(i)
25         j=0
26         matcher1=re.findall(pattern1,i)
27         fr=open(‘./%s/%s‘%(zz,i))
28         for line in fr.readlines():
29             try:
30                 # print(len(line))
31                 # if(len(line)==1):#判断,跳过第一行
32                 #     continue
33                 if(j==0):#跳过第一行
34                     # print("xixi")
35
36                     j=j+1
37                     continue
38                 else:
39                     fw.write(matcher1[0]+‘_%s‘%j+‘,‘+line.strip(‘\n‘)+‘\n‘)
40                     j=j+1
41             except:
42                 pass
43     fr.close()
44     fw.close()
45
46 def joint_b1(listdir,zz):#单独拼接
47     if not os.path.exists(‘./new/‘):
48         os.makedirs(‘./new/‘)
49     fw=open(‘./new/%s.csv‘%(zz),‘a‘)
50     for i in listdir:
51         # print(i)
52         j=1
53         matcher1=re.findall(pattern1,i)
54         fr=open(‘./%s/%s‘%(zz,i))
55         for line in fr.readlines():
56
57             # print(len(line))
58             if(len(line)==4):
59                 continue
60             else:
61                 fw.write(line)
62                 # pass
63
64
65
66     fr.close()
67     fw.close()
68
69
70 def sort_joint():
71     # dir_list=[‘b0‘],‘b3‘,‘b4‘,‘b2_idsd‘,‘b3_idcd‘
72
73
74     dir_list=[‘b1‘]#用于编号和拼接,会在new目录下生成编号号码的文件
75     for i in range(len(dir_list)):
76         listdir=get_dir(dir_list[i])
77         # print(listdir)
78         joint_b0(listdir,dir_list[i])
79
80 def joint_only():
81     dir_list=[‘stock2‘]#用于拼接,将需要拼接的放到stock目录下,会在new目录下生成stock文件,然后根据需要修改名称
82     for i in range(len(dir_list)):
83         listdir=get_dir(dir_list[i])
84         # print(listdir)
85         joint_b1(listdir,dir_list[i])
86
87 def updata(zz):
88     fr=open(‘./new/%s‘%(zz))
89     fw=open(‘./new/new.csv‘,‘a‘)
90     for line in fr.readlines():
91         matcher2=re.findall(pattern2,line)
92         if(matcher2):
93             fw.write(matcher2[0]+‘,‘+line)
94
95
96 if __name__=="__main__":
97     sort_joint()
98     # joint_only()
99     # updata(‘b4.csv‘)
时间: 2024-08-24 10:38:21

joint python文件拼接的相关文章

Python 文件拼接

# -*- coding:utf-8 -*- import re import csv file = open('make_setup.cfg', 'w+') with open("tyb.csv", "r", encoding="GBK") as csvfile: # 读取csv文件,返回的是迭代类型 read = csv.reader(csvfile) aa = list(read) file.write('[setup_basic]\n'

五.Python文件IO和路径操作

目录 Python文件IO操作 上下文管理with 路径操作 01 Python文件IO操作: clumn column open 打开 read 读取 write 写入 close 关闭 readline 行读取 readlines 多行读取 seek 文件指针操作 tell 指针位置 打开文件 open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=No

如何编写一个带命令行参数的Python文件

看到别人执行一个支持命令行参数的python文件,瞬间觉得高大上起来.牛逼起来,那么如何编写一个带命令行参数的python脚本呢?不用紧张,下面将简单易懂地让你学会如何让自己的python脚本,支持命令行参数. 首先你要知道python中的sys模块的一些功能: import sys print "the number of python program's argument:",len(sys.argv) print "the value of every argument

Python文件类型

Python的文件类型分为三种:源代码.字节代码.优化代码. 1. 源代码    Python源代码文件,即py脚本文件,由 python.exe 解释,可在控制台下运行.pyw脚本文件是图形用户接口(Graphical user interface)的源文件,专门用来开发图形界面,由 pythonw.exe 解释运行. 2. 字节代码    Python源文件经过编译后生成的pyc文件,即字节文件.它与平台无关,所以可以移植到其他系统上.下面这段脚本可以把 example.py 编译为 exa

Java 文件拼接器

1. 功能描述: 实现指定目录下相同类型的文件拼接成一个文件, 拼接效果如代码所示. 涉及内容: 反射, io, 递归 1.1 xml 的拼接: <!-- \pom.xml --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:

C#调用python文件执行

我的电脑环境是使用.net framework4.5.1,如果在调试过程中调不通请注意 我用的是Visual studion 2017,python组件下载地址:http://ironpython.codeplex.com/releases/view/ 下载的版本是2.7,下载安装完之后记得引入安装路径下的以下三个dll (1)首先先说一个简单的功能,在c#代码中执行python字符串,内容如下: (2)c#调用python文件: 在当前目录下新建一个后缀名为py的文件,文件名为AmoutDis

python学习笔记2—python文件类型、变量、数值、字符串、元组、列表、字典

python学习笔记2--python文件类型.变量.数值.字符串.元组.列表.字典 一.Python文件类型 1.源代码 python源代码文件以.py为扩展名,由pyton程序解释,不需要编译 [[email protected] day01]# vim 1.py #!/usr/bin/python        print 'hello world!' [[email protected] day01]# python 1.py hello world! 2.字节代码 Python源码文件

搭建Python环境与Python文件类型

Linux环境 - 大多Linux发行版均默认安装了Python环境. - 输入Python可启动Python交互模式 - 程序编辑推荐使用VIM Windows环境 - 可下载安装Python的msi包直接安装 - 自带Python的GUI开发环境 - 开发工具很多 # Linux交互界面 [[email protected] ~]# python Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14) [GCC 4.4.7 20120313 (Red

成功解决在Python文件上右键菜单无“Edit with IDLE”选项

我电脑是Win7旗舰版,之前电脑上安装的是Python2.6版本的,前两天为了体验一下Microsoft Excel与Python之间互操作, 下载并安装了DataNitro,在安装的时候脑残的安装了Python2.7.5,但是这两天在用的时候,发现之前安装的一些包后不能用了: print sys.path 才知道,Python的搜索路径变成了Python2.7.5的路径了,傻眼了,怎么办? 于是,一通百度,google之后,折腾了一个上午,终于搞定了: 首先,我在cmd里面是可以正常使用Pyt