Created
December 26, 2012 13:37
-
-
Save SFantasy/4380381 to your computer and use it in GitHub Desktop.
合并两个文件,如:将test1.txt中的所有行添加到test2.txt的每个行的最末尾。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/python | |
#Filename: mergeFile.py | |
#Usage: merge all the lines in test1 and test2 | |
file1 = open('test1.txt', 'r') | |
file2 = open('test2.txt', 'r') | |
lines1 = file1.readlines() | |
lines2 = file2.readlines() | |
list1 = [] | |
list2 = [] | |
count = 0 | |
for i in lines1: | |
newLine = i.split(',') | |
newLine = [int(p) for p in newLine] | |
list1.append(newLine) | |
print list1 | |
for i in lines2: | |
newLine = int(i) | |
list1[count].append(newLine) | |
count += 1 | |
with open('result.txt', 'w') as f: | |
f.writelines(str(list1)) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment