从另一个文本文件中替换文本文件中的行
我是新来的python,我尝试了一个新问题,但无法获得解决方案。我已经叫replace.txt与内容像这样的文本文件:从另一个文本文件中替换文本文件中的行
81, 40.001, 49.9996, 49.9958
82, 41.1034, 54.5636, 49.9958
83, 44.2582, 58.1856, 49.9959
84, 48.7511, 59.9199, 49.9957
85, 53.4674, 59.3776, 49.9958
86, 57.4443, 56.6743, 49.9959
87, 59.7, 52.4234, 49.9958
现在我多了一个文件名为实际数据,它有一个巨大的像上面这样一个数据量现在我想更换以上通过在actualdata.txt匹配像搜索“81”的第一数目与具有“81”线replace.txt
这里替换它在actualdata.txt线actualdata.txt看起来像这样:
--------lines above--------
81, 40.0 , 50.0 , 50.0
82, 41.102548189607, 54.564575695695, 50.0
83, 44.257790830341, 58.187003960661, 50.0
84, 48.751279796738, 59.921728571875, 50.0
85, 53.468166336575, 59.379329520912, 50.0
86, 57.445611860313, 56.675542227082, 50.0
87, 59.701750075154, 52.424055585018, 50.0
88, 59.725876387298, 47.674633684987, 50.0
89, 57.511209176153, 43.398353484768, 50.0
90, 53.558991157616, 40.654756186166, 50.0
91, 48.853051436724, 40.06599229952 , 50.0
92, 44.335578609695, 41.75898487363 , 50.0
93, 41.139049269956, 45.364964707822, 50.0
94, 4.9858306110506, 4.9976785333108, 50.0
95, 9.9716298556132, 4.9995886389273, 50.0
96, 4.9712790759448, 9.9984071508336, 50.0
97, 9.9421696473295, 10.002460334272, 50.0
98, 14.957223264745, 5.0022762348283, 50.0
99, 4.9568005100444, 15.000751982196, 50.0
------lines below----------
我该怎么做,请帮助我我试图使用fileinput和取代,但我没有得到输出。
这是示例代码我还在即兴(一条线,这是工作的Fyn):
oldline=' 82, 41.102548189607, 54.564575695695, 50.0'
newline=' 81, 40.001, 49.9996, 49.9958'
for line in fileinput.input(inpfile, inplace = 1):
print line.replace(oldline,newline),
这是我最后写的代码:
replacefile= open('temp.txt','r')
for line1 in replacefile:
newline = line1.rstrip()
rl=newline
rl=rl.split()
search =rl[0]
with open(inpfile) as input:
intable = False
for line in input:
fill=[]
if line.strip() == "*NODE":
intable = True
if line.strip() == "---------------------------------------------------------------":
intable = False
if intable:
templine=(line.rstrip())
tl=line.rstrip()
tl= tl.split()
if tl[0] == search:
oldline=templine
for line2 in fileinput.input(inpfile, inplace = 1):
line2.replace(oldline,newline)
但我不能没有得到输出actualdata.txt的内容正在获得deletd,帮助我这个 输出我想要的是改变像这样actualdata.txt:
-------lines above------
81, 40.001, 49.9996, 49.9958
82, 41.1034, 54.5636, 49.9958
83, 44.2582, 58.1856, 49.9959
84, 48.7511, 59.9199, 49.9957
85, 53.468166336575, 59.379329520912, 50.0
86, 57.445611860313, 56.675542227082, 50.0
87, 59.701750075154, 52.424055585018, 50.0
88, 59.725876387298, 47.674633684987, 50.0
89, 57.511209176153, 43.398353484768, 50.0
90, 53.558991157616, 40.654756186166, 50.0
-------lines below------
是replace.txt
也是大?如果没有,你可以先加载到内存中,构建一个字典,并用它在actualdata.txt
我更换线就是我正在做的事情:
-
首先打开
replace.txt
,并建立一个字典。由于您正在用线条的第一个值替换线条,因此我们将其作为字典键。而其价值将是你想要取代的行。像:replacement_data = { '81': '81, 40.001, 49.9996, 49.9958', '82': 82, 41.1034, 54.5636, 49.9958, ... ... }
-
接下来我们开始阅读由线
actualdata.txt
文件,行。所以,我们必须找到这一行的第一个数字是否被替换。所以,我们首先将它拆分为,
,得到第一个字符,看它是否存在于replacement_data
字典中。如果它存在,我们将取代它,如果不存在,我们将简单地忽略。line = "83, 44.257790830341, 58.187003960661, 50.0" first_char = line.split(',')[0].strip() #first char is 83 # lets check whether to replace it or not if first_char in replacement_data.keys(): # if key exists, we have to replace line = replacement_data[first_char] print line # so that it writes to file
把所有的拼在一起:
import fileinput
import sys
inpfile = 'actualdata.txt'
replacement_file = 'replace.txt'
replacement_data = {}
with open(replacement_file) as f:
for line in f:
key = line.split(',')[0].strip()
replacement_data[key] = line
for line in fileinput.input(inpfile, inplace = 1):
first_char = line.split(',')[0].strip()
try:
int(first_char)
line = replacement_data[first_char]
print line,
except (ValueError, KeyError):
print line,
continue
这生成原始文件:
--------lines above--------
81, 40.001, 49.9996, 49.9958
82, 41.1034, 54.5636, 49.9958
...
...
86, 57.4443, 56.6743, 49.9959
87, 59.7, 52.4234, 49.9958 88, 59.725876387298, 47.674633684987, 50.0
89, 57.511209176153, 43.398353484768, 50.0
...
99, 4.9568005100444, 15.000751982196, 50.0
------lines below----------
耶replace.txt非常大,以及actualdata.txt也很大,现在我测试脚本,让你立刻知道有关结果@avi – user3887331 2014-10-17 05:55:40
你得到打印的著名问题'结束,'。所以基本上,你的解决方案将打印“87和88”在同一行:)。不错的尝试 – 2014-10-17 05:56:51
谢谢你,它的工作终于 – user3887331 2014-10-17 05:58:42
使用fileinput模块更换线路就地:
import fileinput
def get_next_line(fptr):
x = fptr.readline()
if(x != ''):
return x.strip(), x.strip().split()[0].strip()
else:
return '',''
f = open("replace.txt", "r")
f_line, f_line_no = get_next_line(f)
for line in fileinput.input("actualdata.txt", inplace=True):
if(line.strip().split()[0].strip() == f_line_no): #if line number matches
print(f_line) # write newline
f_line, f_line_no = get_next_line(f) # Get next newline
else: # Otherwise
print(line.strip()) # write original one
顺便说一下,我使用的是python3。请如果您使用python2
你可以改变你的文本文件到csv适当的修改?我的意思是,既然你有一个分隔符,我认为'csv'可以帮助你以比读取文件操作更简单的方式。 – 2014-10-17 03:58:17
我无法控制它,因为这是它以.txt格式提供的软件的输出文件而已@LearningNeverStops – user3887331 2014-10-17 04:06:53
好的,顺便说一句,你尝试了什么?你提到你使用的文件输入。你能提供代码吗? – 2014-10-17 04:14:52