python读取多个文件拼凑数据

一、需求

11.txt

search a_datetime_x from dpl11294
search c_long from dpl11294
search c_int from dpl11294
search a_long_x from dpl11294
search c_suffix from dpl11294
search a_int from dpl11294
search a_boolean from dpl11294
search a_float from dpl11294
search a_double from dpl11294
search a_boolean_x from dpl11294
search a_string_x from dpl11294
search a_int_x from dpl11294
search c_month_z from dpl11294
search a_double_x from dpl11294
search c_text from dpl11294
search a_long from dpl11294
search c_boolean from dpl11294
search c_city_suffix from dpl11294
search c_month_e from dpl11294
search a_string from dpl11294
search a_float_x from dpl11294
search c_am_pm from dpl11294
search a_datetime from dpl11294
search c_province from dpl11294
search c_date_time from dpl11294
search c_phone_prefix from dpl11294
search a_zero from dpl11294

12.txt 里面是sql字段的别名

'a日期类型x'
'类型long'
'整型'
'a类型longx'
'公司类型'
'数组整型'
'布尔类型数组'
'数组float类型'
'数组double类型'
'数组布尔类型x'
'数组字符型x'
'数组整型x'
'中文月份'
'数组doublex'
'段落信息'
'数组long'
'布尔类型'
'城市类型'
'英文月份'
'数组字符型'
'数组floatx'
'上午下午'
'数组日期类型'
'省份'
'日期类型'
'号码段'
'空数组'

现在需要将这两个文件的内容进行合并,行行对应,例如这种样子:

search a_datetime_x as 'a日期类型x' from dpl11294
二、思路

python读取11.txt文件的每一行,根据from进行拆分,将拆分的数据分别存到数组。读取12.txt文件的每一行,将其存入数组。

因为这三个数组长度是一致的,对任一数组进行遍历,将其拼接,并存到一个文件中。大功告成。

三、python的命令行参数

Python 中也可以所用 syssys.argv 来获取命令行参数:

  • sys.argv 是命令行参数列表。
  • len(sys.argv) 是命令行参数个数。
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys

print '参数个数为:', len(sys.argv), '个参数。'
print '参数列表:', str(sys.argv)

执行以上代码,输出结果为:

$ python test.py arg1 arg2 arg3
参数个数为: 4 个参数。
参数列表: ['test.py', 'arg1', 'arg2', 'arg3']
四、实现
#!/usr/bin/env python
# -- coding: utf-8 --
import fileinput
import sys

# 数组,用于存储 表名
tableArr = []
# 数组,用于存储 操作内容
operateArr = []
# 数组,用于存储 as内容
asArr = []


def readTxt():
    for line in fileinput.input(sys.argv[1]):
        line_data = line.split('from')
        operate = line_data[0]
        table = line_data[1]
        operateArr.append(operate)
        tableArr.append(table)
        pass


def readOtherTxt():
    for line in fileinput.input(sys.argv[2]):
        # 去除回车,并将每一行的数据放入到数组中
        asArr.append(line.strip('\r\n'))
        pass


def merge():
    fo = open(sys.argv[3], "w")
    for index, item in enumerate(operateArr):
        fo.write(item + "as " + asArr[index] + " from" + tableArr[index])
    fo.close()


if __name__ == '__main__':
    readTxt()
    readOtherTxt()
    merge()

python merge.py 11.txt 12.txt 13.txt
五、展示成果

13.txt

search a_datetime_x as 'a日期类型x' from dpl11294
search c_long as '类型long' from dpl11294
search c_int as '整型' from dpl11294
search a_long_x as 'a类型longx' from dpl11294
search c_suffix as '公司类型' from dpl11294
search a_int as '数组整型' from dpl11294
search a_boolean as '布尔类型数组' from dpl11294
search a_float as '数组float类型' from dpl11294
search a_double as '数组double类型' from dpl11294
search a_boolean_x as '数组布尔类型x' from dpl11294
search a_string_x as '数组字符型x' from dpl11294
search a_int_x as '数组整型x' from dpl11294
search c_month_z as '中文月份' from dpl11294
search a_double_x as '数组doublex'from dpl11294 from dpl11294
search c_text as '段落信息' from dpl11294
search a_long as '数组long' from dpl11294
search c_boolean as '布尔类型' from dpl11294
search c_city_suffix as '城市类型' from dpl11294
search c_month_e as '英文月份' from dpl11294
search a_string as '数组字符型' from dpl11294
search a_float_x as '数组floatx' from dpl11294
search c_am_pm as '上午下午' from dpl11294
search a_datetime as '数组日期类型' from dpl11294
search c_province as '省份' from dpl11294
search c_date_time as '日期类型' from dpl11294
search c_phone_prefix as '号码段' from dpl11294
search a_zero as '空数组' from dpl11294
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

create17

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值