[英]How can I replace a large portion of a text in a file using Python?
I am required to use Python 2.3, and I have almost no programming experience. 我需要使用Python 2.3,并且几乎没有编程经验。
I have a large number of files that look exactly like the one below. 我有大量看起来与下面的文件完全相同的文件。 I want to replace the lines following the "0 2" with other ones which look basically the same but have different numbers. 我想将“ 0 2”之后的行替换为其他看起来基本相同但编号不同的行。
Any suggestions? 有什么建议么? Thanks for the help! 谢谢您的帮助!
%chk=Li-000+NT-4-SP-B3LYP-6-31+Gx_chk.chk
# ub3lyp/6-31+G* scf=xqc geom=connectivity
Li-000+NT-4-SP-B3LYP-6-31+Gx_chk
0 2
C
C 1 2.23392993
C 2 2.23392993 1 90.00000000
C 3 2.23392993 2 90.00000000 1 0.00000000 0
C 4 2.45854269 3 103.13597413 2 62.19088628 0
C 5 1.41263246 4 30.96712274 3 -114.96688053 0
C 5 2.47345678 4 155.57277852 3 -3.95739525 0
C 7 1.40192800 5 31.36445834 4 -67.05177346 0
C 8 1.47023488 7 118.88012120 5 133.96696254 0
C 9 1.41263246 8 119.23313037 7 178.98716443 0
C 7 2.38756302 5 101.23515048 4 3.92909825 0
C 11 1.40192800 7 90.00000000 5 -29.47652788 0
C 12 1.47023488 11 118.88012120 7 111.98348127 0
C 13 1.41263246 12 119.23313037 11 178.98716443 0
C 11 2.38756302 7 90.00000000 5 60.52347212 0
C 15 1.40192800 11 90.00000000 7 -90.00000000 0
C 16 1.47023488 15 118.88012120 11 111.98348127 0
C 17 1.41263246 16 119.23313037 15 178.98716443 0
C 15 2.38756302 11 90.00000000 7 0.00000000 0
C 19 1.40192800 15 90.00000000 11 -90.00000000 0
C 19 2.48710480 15 101.72503377 11 60.64513898 0
C 21 1.41263246 19 31.05462349 15 -108.16385345 0
C 21 1.44360564 19 134.93861618 15 -37.90663231 0
C 23 1.44360564 21 112.67175768 19 -1.82333415 0
C 24 1.41263246 23 118.80062827 21 -31.82335089 0
C 24 1.44360564 23 101.38056080 21 100.35603013 0
C 26 1.44360564 24 112.67175768 23 -100.35603013 0
C 27 1.41263246 26 118.80062827 24 -31.82335089 0
C 27 1.44360564 26 101.38056080 24 100.35603013 0
C 29 1.44360564 27 112.67175768 26 -100.35603013 0
C 30 1.41263246 29 118.80062827 27 -31.82335089 0
C 21 1.44360564 19 87.91301483 15 66.13240820 0
H 1 1.08676157 6 120.73303602 5 174.96459862 0
H 4 1.08676157 3 93.81889728 2 -93.82740845 0
H 2 1.08676157 1 93.81889728 6 -126.09599138 0
H 3 1.08676157 2 93.81889728 1 -93.82740845 0
H 29 1.08676157 27 120.73303602 26 52.85602036 0
H 26 1.08676157 24 120.73303602 23 52.85602036 0
H 32 1.08676157 21 120.73303602 19 171.61058920 0
H 23 1.08676157 21 120.73303602 19 151.38871634 0
X 15 1.82799866 11 49.22775544 7 -30.42058102 0
Li 41 0.00000001 15 112.54815565 11 -110.97981054 0
1 6 1.5 10 1.5 33 1.0
2 10 1.5 14 1.5 35 1.0
3 14 1.5 18 1.5 36 1.0
4 6 1.5 18 1.5 34 1.0
5 6 1.5 8 1.0 20 1.0
6
7 8 1.5 22 1.0 25 1.0
8 9 1.0
9 10 1.5 12 1.0
10
11 12 1.5 25 1.0 28 1.0
12 13 1.0
13 14 1.5 16 1.0
14
15 16 1.5 28 1.0 31 1.0
16 17 1.0
17 18 1.5 20 1.0
18
19 20 1.5 22 1.0 31 1.0
20
21 22 1.5 23 1.5 32 1.5
22
23 24 1.5 40 1.0
24 25 1.5 26 1.5
25
26 27 1.5 38 1.0
27 28 1.5 29 1.5
28
29 30 1.5 37 1.0
30 31 1.5 32 1.5
31
32 39 1.0
33
34
35
36
37
38
39
40
41
fpath = os.path.join(thisdir, filename)
with open(fpath, 'r+') as f:
lines = f.readlines()
f.seek(0)
f.truncate()
for line in lines:
if '0 2' in line:
for test in filelist:
pathmatch = file_match(line, test)
if pathmatch is not None:
repstring = filelist[test] + pathmatch
line = line.replace(test, repstring)
f.write(line)
one weird thing is you will have to make copy of you files before running above code otherwise you may loose your original code 一件奇怪的事情是,在运行上述代码之前,您必须先复制文件,否则可能会丢失原始代码
A (relatively) safe way to replace a line in a file. 一种(相对)安全的方式替换文件中的行。
enter code here
#!/usr/bin/python
# defensive programming style
# function to replace a line in a file
# and not destroy data in case of error
def replace_line(filepath, oldline, newline ):
"""
replace a line in a temporary file,
then copy it over into the
original file if everything goes well
"""
# quick parameter checks
assert os.exists(filepath) # !
assert ( oldline and str(oldline) ) # is not empty and is a string
assert ( newline and str(newline) )
replaced = False
written = False
try:
with open(filepath, 'r+') as f: # open for read/write -- alias to f
lines = f.readlines() # get all lines in file
if oldline not in lines:
pass # line not found in file, do nothing
else:
tmpfile = NamedTemporaryFile(delete=True) # temp file opened for writing
for line in lines: # process each line
if line == oldline: # find the line we want
tmpfile.write(newline) # replace it
replaced = True
else:
tmpfile.write(oldline) # write old line unchanged
if replaced: # overwrite the original file
f.seek(0) # beginning of file
f.truncate() # empties out original file
for tmplines in tmpfile:
f.write(tmplines) # writes each line to original file
written = True
tmpfile.close() # tmpfile auto deleted
f.close() # we opened it , we close it
except IOError, ioe: # if something bad happened.
printf ("ERROR" , ioe)
f.close()
return False
return replaced and written # replacement happened with no errors = True
(note: this replaces entire lines only , and all of the lines that match in the file) (注意:这仅替换整行,以及文件中所有匹配的行)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.