一、需求

11.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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字段的别名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'a日期类型x'
'类型long'
'整型'
'a类型longx'
'公司类型'
'数组整型'
'布尔类型数组'
'数组float类型'
'数组double类型'
'数组布尔类型x'
'数组字符型x'
'数组整型x'
'中文月份'
'数组doublex'
'段落信息'
'数组long'
'布尔类型'
'城市类型'
'英文月份'
'数组字符型'
'数组floatx'
'上午下午'
'数组日期类型'
'省份'
'日期类型'
'号码段'
'空数组'

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

1
search a_datetime_x as 'a日期类型x' from dpl11294

二、思路

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

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

三、python的命令行参数

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

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

import sys

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

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

1
2
3
$ python test.py arg1 arg2 arg3
参数个数为: 4 个参数。
参数列表: ['test.py', 'arg1', 'arg2', 'arg3']

四、实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/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()
1
python merge.py 11.txt 12.txt 13.txt

五、展示成果

13.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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