python實(shí)現(xiàn)兩個(gè)文件合并功能
本文將會(huì)分析一個(gè)文件合并的程序,并指出在合并文件過(guò)程中需要注意的問(wèn)題。
下面是需要合并的文件示例:


分析思路:
要將兩個(gè)文件合并,首先要將文件讀到內(nèi)存中,成為列表。再將列表分割,按照類別將數(shù)據(jù)分開存儲(chǔ),即姓名、電話、郵箱。通過(guò)遍歷列表1,依次與列表2比較,如果二者有重合的人,那么直接利用 .join([ ])姓名電話郵箱合并到一行,保存到另外一個(gè)列表變量里面。二者不重合的人,說(shuō)明只有列表1中才有,故將此人的郵箱信息用str(‘—–')代替。
經(jīng)過(guò)這次遍歷之后,列表1中所有的人和列表2中與列表1中重復(fù)的人,都重新整合到了新的列表變量里面。接下來(lái)還需要把列表2中特有的人,添加到新的列表變量中。方法是遍歷列表2,把與列表1不重合的人取出來(lái)保存。
最后要將新的列表變量中的數(shù)據(jù)寫入到新的文件中,并關(guān)閉所有文件。
流程圖如下:

代碼如下:
"""
Created on Fri Aug 4 12:59:36 2017
@author: 13323
"""
# This program can combine two or more files into one file.
def main():
#firstly open the files
data1 = open("test_3.txt","rb")
data2 = open("test_4.txt","rb")
# read the data in file into list
data1.readline() #only read one line, skip the first line
data2.readline() #only read one line, skip the first line
file1 = data1.readlines() #read all variable into list file1
file2 = data2.readlines() #read all variable into list file2
#print(file1)
#define particular list to store variable
file1_name = []
file1_tel = []
file2_name = []
file2_email = []
#file3 = []
#split file1 into two part
for line in file1:
element = line.split() #line.split(); devide by ' '
file1_name.append(str(element[0].decode('gbk')))
file1_tel.append(str(element[1].decode('gbk')))
#split file2 into two part
for line in file2:
element = line.split()
file2_name.append(str(element[0].decode('gbk')))
file2_email.append(str(element[1].decode('gbk')))
# pick up the name in the file1 same as the name in the file2 and combine
file3 = []
for i in range(len(file1_name)):
s = ''
if file1_name[i] in file2_name:
j = file2_name.index(file1_name[i])
s = '\t'.join([file1_name[i],file1_tel[i],file2_email[j]])
s += '\n'
else:
s = '\t'.join([file1_name[i],file1_tel[i],str("----")])
s += '\n'
file3.append(s)
#pick up the name in the file1 doesn't same as the name in the file2
for i in range(len(file2_name)):
s = ''
if file2_name[i] not in file1_name:
s = '\t'.join([file2_name[i],str('----'),file2_email[i]])
s += '\n'
file3.append(s)
#write the data into file3
data3 = open("test_5.txt","w")
data3.writelines(file3)
#close the file
data1.close()
data2.close()
data3.close()
main()
關(guān)鍵點(diǎn):
編碼與解碼
列表合并與拆解
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python機(jī)器學(xué)習(xí)降低靜態(tài)日志噪聲
今天小編就為大家分享一篇關(guān)于使用Python和機(jī)器學(xué)習(xí)的靜態(tài)日志噪聲的文章,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-09-09
在tensorflow中實(shí)現(xiàn)去除不足一個(gè)batch的數(shù)據(jù)
今天小編就為大家分享一篇在tensorflow中實(shí)現(xiàn)去除不足一個(gè)batch的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
教你用Python實(shí)現(xiàn)簡(jiǎn)易版學(xué)生信息管理系統(tǒng)(含源碼)
學(xué)生管理信息系統(tǒng)主要用來(lái)日常查詢學(xué)生信息,以及及時(shí)更新數(shù)據(jù)和修改數(shù)據(jù).用python實(shí)現(xiàn)簡(jiǎn)單學(xué)生管理信息系統(tǒng)不僅可以滿足以上要求,也可以鞏固之前學(xué)習(xí)的基礎(chǔ),需要的朋友可以參考下2021-06-06
Python按行讀取文件的簡(jiǎn)單實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇Python按行讀取文件的簡(jiǎn)單實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
Django關(guān)于事務(wù)transaction.atomic()的使用方式
這篇文章主要介紹了Django關(guān)于事務(wù)transaction.atomic()的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Python Pymysql實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)的示例
本文主要介紹了Python Pymysql實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

