python - 逐行重新编号
问题描述
我有一个如下所示的输入文本:
word77 text text bla66 word78 text bla67
text bla68 word79 text bla69 word80 text
bla77 word81 text bla78 word92 text bla79 word99
我必须 在每一行中重新编号word
并从 1 开始。bla
我可以重新编号整个输入,如下所示:
word1 text text bla1 word2 text bla2
text bla3 word3 text bla4 word4 text
bla5 word5 text bla6 word6 text bla7 word7
上述代码:
import re
def replace(m): global i; i+=1; return str(i);
fp = open('input.txt', 'r').read()
i = 0
fp = re.sub(r'(?<=word)(\d+)', replace, fp)
i = 0
fp = re.sub(r'(?<=bla)(\d+)', replace, fp)
#open('sample.txt', 'wb').write(fp)
print fp
理想情况下,结果应如下所示:
word1 text text bla1 word2 text bla2
text bla1 word1 text bla2 word2 text
bla1 word2 text bla3 word3 text bla4 word4
解决方案
您一次对整个文件进行操作 ( fp.read()
) - 您需要逐行操作:
with open("input.txt","w") as f:
f.write("""word77 text text bla66 word78 text bla67
text bla68 word79 text bla69 word80 text
bla77 word81 text bla78 word92 text bla79 word99""")
import re
i = 0
def replace(m):
global i
i+=1
return str(i)
with open('input.txt') as fp, open("output.txt","w") as out:
# read only one line of the file and apply the transformations
for line in fp:
i = 0
l = re.sub(r'(?<=word)(\d+)', replace, line)
i = 0
l = re.sub(r'(?<=bla)(\d+)', replace, l)
out.write(l)
with open("output.txt") as f:
print(f.read())
输出:
word1 text text bla1 word2 text bla2
text bla1 word1 text bla2 word2 text
bla1 word1 text bla2 word2 text bla3 word3
推荐阅读
- javascript - JQuery,LI 元素上的 DOM 删除问题
- c# - DivideByZeroException 被抛出为 FatalExecutionEngineError,未捕获
- python - 如何重塑图片numpy数组
- reactjs - 如何使用 firestoreConnect 将多个数据库获取到 redux firestore 数据库?
- ios - 在具有相同行值名称的部分中显示行
- excel - 在宏执行期间提示用户选择单元格,然后将此单元格的值分配给字符串变量
- wix - 自定义操作后 Wix 安装程序刷新页面
- javascript - 如何打开ajax调用返回的网页?
- python - 如何组合这 2 个类似 json 的文件?
- c - 数字频率程序(HackerRank)