求助个问题
#需求测试1
测试2
测试3
测试4
转换成测试1 #1
测试2 #2
测试3 #3
测试4 #4
我自己也写了代码,主要就是将每一行的内容读出,然后就算出文件的总行数,最后拼接就行,但是我的代码好像不大行,实现方式有很多种,希望能在我的思路的方式基础上修改
new_list=[]
with open(r"e.txt","r",encoding="utf-8") as f:
list1 = f.readlines()
count = len(list1)
for i in list1:
for j in range(count):
new_line = i.rstrip() + " #" + str(j+1) +"\n"
new_list.append(new_line) 本帖最后由 人造人 于 2022-1-27 22:39 编辑
#!/usr/bin/env python
#coding=utf-8
new_list=[]
with open(r"e.txt","r",encoding="utf-8") as f:
list1 = f.readlines()
for i in list1:
new_line = i.rstrip() + " #" + str(len(new_list) + 1) + "\n"
new_list.append(new_line)
print(new_list)
'''
with open("f.txt", "w", encoding = "utf-8") as f:
for i in new_list:
f.write(i)
'''
new_list=[]
with open(r"e.txt","r",encoding="utf-8") as f:
list1 = f.readlines()
count = len(list1)
for i, v in enumerate(list1, 1):
new_line = v.rstrip() + " #" + str(i)
new_list.append(new_line)
for i in new_list:
print(i)
页:
[1]