[英]i'm saving data from a txt box to a specific line in a file but instead it saves on the first line
[英]I'm having a problem saving a file that saves the last line python
lines = [line.rstrip('\n') for line in open('1.txt','r')]
for names in lines:
for x in range(100):
y=names.lower()+f'{x:02}'
print(y)
with open('1.txt','w') as f:
f.write(y + '\n')
此代碼讀取文件並將 00 添加到 99 我在將數據保存到同一文件並將舊數據替換為新數據時遇到問題它保存了最后一行我想要一個解決方案,謝謝
如果我正確理解了您的問題,您希望在新文件中將單獨一行中的每個名稱重寫 100 次,並在后面添加數字 00-99。 別說了...
def rewrite_names_v1():
#get list of names
with open('demo.txt','r') as file:
names = [line.strip('\n') for line in file]
with open('demo.txt','w') as file:
for name in names:
lowercase_name = name.lower()
for x in range(100):
result = lowercase_name+f'{x:02}'
# print(result)
if name != names[-1] or x != 99:
file.write(result+'\n')
else:
file.write(result)
if __name__ == '__main__':
rewrite_names_v1() #rewrites file with range(100) as nested loop
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.