python3 如何中将两个文件按行合并

作者&投稿:卞迹 (若有异议请与网页底部的电邮联系)
python3 如何中将两个txt按行生成新内容?~

先写一个函数,按行读文件存放到列表中。

调用这个函数读文件A,存到列表A.
再调用这个函数读文件B,存到列表B。
再写个for循环,每次读一行A列表,一行B列表,然后A和B读出的内容拼接,存到列表C。
最后,将列表C的内容写入文件。

Python编程将两个文件合并,代码如下:
//例子:合并a.txt和b.txt文件def readf(filename): lines = file(filename).readlines() dic = {} for i in lines: i_ = i.split() dic[i_[0]] = int(i_[1]) return dicdica = readf('a.txt')dicb = readf('b.txt')lines = []for i in dica: percent = str(float(dicb[i])*100/dica[i])+'%' s = ' '.join([i, str(dica[i]), str(dicb[i]), percent]) s += '
' lines.append(s)//合并成c.txt with open('c.txt', 'w') as f: f.writelines(lines) f.close()

先将两个文件分别读取到两个列表中,再用循环输出到第3个文件。

f_ = open('e:/python27/a.txt','r')
n=0
list1=[]
for i in f_.readlines():
n+=1
s=i.strip()
list1.append(s)
f_.close()

ff_ = open('e:/python27/b.txt','r')
m=0
list2=[]
for i in ff_.readlines():
m+=1
s=i.strip()
list2.append(s)
ff_.close()

fff_=open('e:/python27/c.txt','w')
for i in range(n):
s=list1[i]+' '+list2[i]
fff_.write(s+'
')
print(s)
fff_.close()


python test.py --input1 dat1.txt --input2 dat2.txt > 2.out.txt
复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'shengwei ma'
__author_email__ = 'shengweima@icloud.com'
import sys
import getopt

input_file1 = ""
input_file2 = ""

try:
opts, args = getopt.getopt(sys.argv[1:], "h", ["input1=", "input2="])
except getopt.GetoptError as err:
print(str(err))
for op, value in opts:
if op == "--input1":
input_file1 = value
elif op == "--input2":
input_file2 = value
elif op == "-h":
print("python get_value_according_first_column.py --input1 dat1 --input2 dat2 > out.txt")
sys.exit()
# 以上可忽略,定义shell中接受的参数及数据

f1 = open(input_file1, 'r')
f2 = open(input_file2, 'r')
lines1 = f1.readlines() # 将整个文件读作一个列表,可以添加 print lines1 查看,这里一行表示里边的一个元素(字符串),如lines1[0],则表示第一行
lines2 = f2.readlines() # 将整个文件读作一个列表,可以添加 print lines2 查看,第一行第一列,lines2[0][0]
for line1 in lines1: # 遍历列表lines1中的每个元素,及遍历读取文件1的每一行
line1 = line1.strip().split() # 这里的一行就是一个字符串,使用字符串的strip方法,去掉行尾换行符,使用split分割字符串成列表
for line2 in lines2:
line2 = line2.strip().split() # 同样 遍历文件2中每一行
if line1[0] in line2: # line1[0] (注意是line 不是lines) 表示某一行的第一列,即查询某行第一列是否在文件2中,如果在
line1.extend(line2[1:]) # 在的话,则将 文件2中的第二列以后的部分添加到第一行的后边
print ' '.join(line1) # 将列表 line1 转换成字符串打印
f1.close() # 关闭文件
f2.close() # 关闭文件
复制代码


岫岩满族自治县15249332798: python3 如何中将两个文件按行合并 -
弭汪健脾: python test.py --input1 dat1.txt --input2 dat2.txt > 2.out.txt 复制代码#!/usr/bin/env python# -*- coding: utf-8 -*- __author__ = 'shengwei ma' __author_email__ = 'shengweima@icloud.com' import sys import getopt input_file1 = "" input_file2 = "" ...

岫岩满族自治县15249332798: python中将两个文件合并 -
弭汪健脾: 你好: 其实这个问题不是很难啊: 请看代码: txtpath1=r"a.txt" txtpath2=r"b.txt" txtpath3=r"c.txt"fpa=open(txtpath1) fpb=open(txtpath2) fpc=open(txtpath3,"w")arrB=[] for lineb in fpb.readlines():arrB.append(lineb)index=0 for linea in ...

岫岩满族自治县15249332798: 用python 如何将两个文档中的内容,整合到一个文档,第二个文档的内容放在第一个文档的右边一列 -
弭汪健脾: changed on "silmerusee" # encoding: utf-8from string import strip from itertools import imapopen('c.txt','w').write('\n'.join(imap('{0} {1}'.format,imap(strip, open('a.txt', 'r')),imap(strip, open('b.txt', 'r')))))

岫岩满族自治县15249332798: python中将两个文件合并 文本A.txt存有 1 2 3 4 同时个文件B存有 a b -
弭汪健脾: with open('a.txt','r') as fa: with open('b.txt','r') as fb: with open('c.txt','w') as fc: for line in fa: fc.write(line) fc.write(fb.readline())

岫岩满族自治县15249332798: 在python中 什么叫 “将两个print()调用替换为新文件的I/O代码,然后运行程序,确认确实创建了数据文件”? -
弭汪健脾: 就是将print语句的输出不输出到屏幕上,而是输出到文件中 在python3中在print语句中加上 file="数据文件名"这个参数即可

岫岩满族自治县15249332798: python 多个文件怎么调用 -
弭汪健脾: 同时打开三个文件,文件行数一样,要求实现每个文件依次读取一行,然后输出,我们先来看比较容易想到的写法:with open(filename1, 'rb') as fp1: with open(filename2, 'rb') as fp2: with open(filename3, 'rb') as fp3: for i in fp1: j = fp2.readline() ...

岫岩满族自治县15249332798: python如何调用另一个py文件的所有函数 -
弭汪健脾: 在同一个文件夹下调用函数: A.py文件: B.py文件: 或 调用类: A.py文件: B.py文件: 或 在不同文件夹下 A.py文件的文件路径:E:\PythonProject\winycg B.py文件:针对这个问题,网上有很多的解决方式.其实最主要的原...

岫岩满族自治县15249332798: 一台电脑如何同时使用python2idle和python3idle吗 -
弭汪健脾: 一台电脑可以同时使用python2idle和python3idle,就是在系统中装两个版本的python,这样在文件右键菜单中就会出现两个IDLE,最新版python3.5.1已经可以显示版本号, python2.x的脚本就可以用python2idle编辑,python3.x的脚本就可以用python3idle编辑.以下为安装两个版本时右键菜单的显示,上方为python2.7.11的IDLE,下方为3.5.1版本的:

岫岩满族自治县15249332798: 用python如何提取出两个文本文件中有共同部分的那行,如: 第一个文档a.txt中有 1, 2, -
弭汪健脾: 如果是b文件中的行包含a文件中的行,可以这样 alines = open('a.txt','r').readlines() fw = open('c.txt','w') for line in open('b.txt','r'): for al in alines: if al[:-1] in line: fw.write(line) fw.close()

岫岩满族自治县15249332798: python3 file open默认以什么方式打开+csdn -
弭汪健脾: python:open/文件操作 open/文件操作 f=open('/tmp/hello','w')#open(路径+文件名,读写模式)#读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式 如:'rb','wb','r+b'等等 读写模式的类型有:rU 或 Ua 以读方式...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网