2014-02-07 87 views
1

我需要使用Python 2.3,而且我幾乎沒有編程經驗。如何使用Python替換文件中的大部分文本?

我有大量的文件看起來如下所示。我想用其他看起來基本相同但具有不同數字的行代替「0 2」後面的行。

有什麼建議嗎?謝謝您的幫助!

%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 
+0

(1)讀出的文本行到存儲器(2)取代所期望的線(3)將文本寫出到新文件中。這將是一個很好的開始:https://stackoverflow.com/questions/4778697/how-to-replace-update-text-in-a-file-line-by-line – hughdbrown

+0

首先你真的需要使用python的這份工作?如果我的要求是正確的,那麼也可以用perl命令行來完成。「perl -i -pe's/^ 0 2/0 1/g'*」。 – Arunmu

+0

@ArunMu「我需要使用python 2.3」 – jayelm

回答

0
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) 

一個奇怪的是,你必須讓你的文件的副本,上面的代碼運行,否則你可能會失去原來的代碼

(相對)安全的方式之前,在更換線文件。

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 

(注意:這僅替換整個線,並且所有匹配文件中的所有行的)