我有一个大文本文件,
zhang
zhipeng
peng
我希望在 N 行之前让它在文本文件中写入str “ - ”。
zhang
--zhipeng
peng
我不希望将所有文件读取到内存中,因为文件太大了。 有点类似标记文件的某些行。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>/opt/work/users/zhanghzipeng/baidu_baike/baike/utils/text_helper.py.html</title>
<meta name="Generator" content="Vim/7.2">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#000000" text="#ffffff"><font face="monospace">
<font color="#ffff00"> 1 </font><font color="#00ffff"># -*- coding:utf-8 -*-</font><br>
<font color="#ffff00"> 2 </font><br>
<font color="#ffff00"> 3 </font><br>
<font color="#ffff00"> 4 </font><font color="#00ffff"># @version: 1.0</font><br>
<font color="#ffff00"> 5 </font><font color="#00ffff"># @author: Zhipeng Zhang</font><br>
<font color="#ffff00"> 6 </font><font color="#00ffff"># @date: '2015/5/28'</font><br>
<font color="#ffff00"> 7 </font><font color="#ff40ff">import</font> os<br>
<font color="#ffff00"> 8 </font><br>
<font color="#ffff00"> 9 </font><font color="#ff40ff">import</font> fileinput<br>
<font color="#ffff00">10 </font><br>
<font color="#ffff00">11 </font><br>
<font color="#ffff00">12 </font><font color="#ffff00">def</font> <font color="#00ffff">rewrite_line</font>(filename, lineno, text, left=True, right=False):<br>
<font color="#ffff00">13 </font> f = fileinput.input(filename, inplace=1)<br>
<font color="#ffff00">14 </font> <font color="#ffff00">for</font> line <font color="#ffff00">in</font> f:<br>
<font color="#ffff00">15 </font> line_text = line.replace("<font color="#ff40ff">\r\n</font>", "").replace("<font color="#ff40ff">\n</font>", "")<br>
<font color="#ffff00">16 </font> <font color="#ffff00">if</font> f.lineno() == lineno:<br>
<font color="#ffff00">17 </font> <font color="#ffff00">if</font> left:<br>
<font color="#ffff00">18 </font> <font color="#ffff00">print</font> text + line_text<br>
<font color="#ffff00">19 </font> <font color="#ffff00">elif</font> right:<br>
<font color="#ffff00">20 </font> <font color="#ffff00">print</font> line_text + text<br>
<font color="#ffff00">21 </font> <font color="#ffff00">else</font>:<br>
<font color="#ffff00">22 </font> <font color="#ffff00">print</font> text<br>
<font color="#ffff00">23 </font> <font color="#ffff00">else</font>:<br>
<font color="#ffff00">24 </font> <font color="#ffff00">print</font> line_text<br>
<font color="#ffff00">25 </font> f.close()<br>
<font color="#ffff00">26 </font><br>
<font color="#ffff00">27 </font><font color="#ffff00">def</font> <font color="#00ffff">cat</font>(filename):<br>
<font color="#ffff00">28 </font> <font color="#ffff00">with</font> file(filename) <font color="#ff40ff">as</font> f:<br>
<font color="#ffff00">29 </font> <font color="#ffff00">print</font> f.read()<br>
<font color="#ffff00">30 </font> os.remove(filename)<br>
<font color="#ffff00">31 </font> <font color="#ffff00">with</font> file(filename, "<font color="#ffff00">w</font>")<font color="#ff40ff">as</font> f:<br>
<font color="#ffff00">32 </font> f.write("<font color="#ffff00">a</font><font color="#ff40ff">\n</font><font color="#ffff00">b</font><font color="#ff40ff">\n</font><font color="#ffff00">c</font>")<br>
<font color="#ffff00">33 </font><br>
<font color="#ffff00">34 </font><font color="#ffff00">def</font> <font color="#00ffff">test</font>():<br>
<font color="#ffff00">35 </font> filename = "<font color="#ffff00">rewrite.test</font>"<br>
<font color="#ffff00">36 </font> <font color="#ffff00">with</font> file(filename, "<font color="#ffff00">w</font>")<font color="#ff40ff">as</font> f:<br>
<font color="#ffff00">37 </font> f.write("<font color="#ffff00">a</font><font color="#ff40ff">\n</font><font color="#ffff00">b</font><font color="#ff40ff">\n</font><font color="#ffff00">c</font>")<br>
<font color="#ffff00">38 </font><br>
<font color="#ffff00">39 </font> <font color="#ffff00">print</font> "<font color="#ffff00">new file:</font>" + filename<br>
<font color="#ffff00">40 </font> <font color="#ffff00">print</font> "<font color="#ffff00">file content: a</font><font color="#ff40ff">\n</font><font color="#ffff00">b</font><font color="#ff40ff">\n</font><font color="#ffff00">c</font>"<br>
<font color="#ffff00">41 </font><br>
<font color="#ffff00">42 </font> <font color="#ffff00">print</font> "<font color="#ffff00">--------start test---------</font>"<br>
<font color="#ffff00">43 </font><br>
<font color="#ffff00">44 </font> <font color="#ffff00">print</font> filename, "<font color="#ffff00">line:</font>", 2, "<font color="#ffff00">left add # --> #b?</font>"<br>
<font color="#ffff00">45 </font> rewrite_line(filename, 2, "<font color="#ffff00">#</font>", left=True)<br>
<font color="#ffff00">46 </font> cat(filename)<br>
<font color="#ffff00">47 </font><br>
<font color="#ffff00">48 </font><br>
<font color="#ffff00">49 </font> <font color="#ffff00">print</font> filename, "<font color="#ffff00">line:</font>", 2, "<font color="#ffff00">right add #END --> b#END?</font>"<br>
<font color="#ffff00">50 </font> rewrite_line(filename, 2, "<font color="#ffff00">#END</font>", left=False, right=True)<br>
<font color="#ffff00">51 </font> cat(filename)<br>
<font color="#ffff00">52 </font><br>
<font color="#ffff00">53 </font> <font color="#ffff00">print</font> filename, "<font color="#ffff00">line:</font>", 1, "<font color="#ffff00"> rewrite a --> zhipeng?</font>"<br>
<font color="#ffff00">54 </font> rewrite_line(filename, 1, "<font color="#ffff00">zhipeng</font>", left=False, right=False)<br>
<font color="#ffff00">55 </font> cat(filename)<br>
<font color="#ffff00">56 </font><br>
<font color="#ffff00">57 </font> os.remove(filename)<br>
<font color="#ffff00">58 </font><br>
<font color="#ffff00">59 </font><br>
<font color="#ffff00">60 </font><font color="#ffff00">if</font> __name__ == "<font color="#ffff00">__main__</font>":<br>
<font color="#ffff00">61 </font> test()<br>
</font></body>
</html>
答案 0 :(得分:0)
# implement
def mark(ifn, ofn, linenum):
with open(ifn, "rt") as ifile:
with open(ofn, "wt") as ofile:
n = 0
for line in ifile.readlines():
n = n + 1
if n == linenum:
ofile.write("---")
ofile.write(line)
# execute
mark("a.txt", "b.txt", 4)