2017-07-14 76 views
0

我对python非常陌生,远离编写自己的脚本。 对于我使用lilypond的工作,我需要一个解析文本文件的脚本, 搜索字符串并在匹配之前插入另一个文本文件。 我一直在寻找这个有点脚本很多,我没有找到任何。Python脚本在文本文件中搜索字符串并在匹配之前插入另一个文本文件

所以我结束了我结合在这里和其他网站发现snipets以及与此脚本上来,这是工作:

#!/usr/bin/env python 

# usage: 
# $ python thisfile.py text.txt searchstring insert.txt 

import sys 

f2 = open(sys.argv[3]) 
data = f2.read() 
f2.close() 

with open(sys.argv[1], "r+") as f1: 
    a = [x.rstrip() for x in f1] 
    index = 0 
    for item in a: 
     if item.startswith(sys.argv[2]): 
      a.insert(index, data) 
      break 
     index += 1 
    f1.seek(0) 
    f1.truncate() 
    for line in a: 
     f1.write(line + "\n") 

,虽然我做了这个工作,我远离理解详细说明那里发生了什么,如果它是好的,如果不是如何使它更好。

+2

我建议您访问[代码审查。(https://codereview.stackexchange.com/) –

+0

不错,谢谢!我在这里是全新的,并不知道它存在... – nath

回答

1

我会尽力解释每一行所做的:

#!/usr/bin/env python 

# usage: 
# $ python thisfile.py text.txt searchstring insert.txt 

# this line just imports the sys library used here to read command line arguments 
import sys 

# This opens the file provided has the 3rd argument. 
# By default, it will open the file just for reading. 
f2 = open(sys.argv[3]) 
# This just reads the contents of the file 
# and stores them in-memory as `data` 
data = f2.read() 
# This closes the file. 
# Essentially telling the OS that you are done with the file. 
# After this, trying to read the file again would raise an error. 
f2.close() 

# the `with ... as ...:` provides another way of reading a file. 
# the file will be automatically closed when the program exits this block (even when an error occurs) 
# 
# the second parameter "r+" passed to `open` 
# tells the OS that you'll need to read and write to this file. 
with open(sys.argv[1], "r+") as f1: 
    # This is using list comprehension to convert`f1` into a list 
    # where every line in f1 will be an element of the list (`for x in f1` or `for line in f1`) 
    # additionally, `rstrip()` is also being called for every line 
    # this strips all trailling white space from each line (including the new line character) 
    a = [x.rstrip() for x in f1] 

    index = 0 
    # this will iterate through every element of `a` (essentially every line of `f1`) 
    for item in a: 
     # is this line starts with the given pattern 
     if item.startswith(sys.argv[2]): 
      # insert the data read in the beginning into the current index of `a` 
      # this pushes all remaining lines forward 
      a.insert(index, data) 
      # since the match was found, break out of the for loop 
      break 
     # at every iteration of the for loop, keep track of the current index 
     index += 1 

    # this return to the beginning of the file 
    # since `a = [x.rstrip() for x in f1]` moved you to the end of file 
    f1.seek(0) 
    # this clears all the remaining content of `f1`. 
    # since we just returned to the beginning, clears the file 
    f1.truncate() 
    # for every line in `a` (that now includes the data read from `f2` 
    for line in a: 
     # write it to `f1`, reintroducing the new line character "\n" 
     f1.write(line + "\n") 
+0

**真棒,非常感谢这一个,**这使我很清楚,但也显示脚本的问题。如果从_f1_或_f2_读取数据时出现任何错误,_f1.truncate()_将删除_f2_的原始内容,则无法(重新)写入适当的内容,内容将丢失。更安全的方法是使用某种临时文件,或者至少在调用_truncate()_之前移动_f1_的原始内容。 – nath

+0

在[Code Review](https://codereview.stackexchange.com)中有一个[详细解释问题](https://codereview.stackexchange.com/questions/169324/python-script-searching-for -string-in-textfile-and-inserted-another-textfile-be)以及[Gareth Rees]的替代代码(https://codereview.stackexchange.com/users/11728/gareth-rees)。 – nath

相关问题