[英]Create a comma-separated number string within a.txt file
I would like you to help me insert into a file.txt called useless.txt a comma-separated number sequence, follows the code.我想请您帮我在名为 useless.txt 的 file.txt 中插入一个逗号分隔的数字序列,如下代码所示。 Example that should be inside the file: 0001 => 1,2,3,4,5,6,7,8 I can't put the information I want into the file.
应该在文件中的示例: 0001 => 1,2,3,4,5,6,7,8 我无法将我想要的信息放入文件中。 the file is blank.
该文件是空白的。
cont = 0
def contador(i,f,p):
print(f'A contagem de {i} até {f} de {p} em {p}')
if i < f:
cont = i
print('\n\n000=>')
while cont <= f:
print(f'{cont}', end=' ')
cont += p
print('FIM!')
else:
cont = i
while cont >= f:
print(f'{cont}', end=',')
cont -= p
print('FIM!')
print('Agora é sua vez, personalize um contador:')
i = int(input('Inicio: '))
f = int(input('Fim: '))
p= int(input('Passo: '))
arquivo = open(input('Nome do arquivo a ser editado:'), 'r')
texto = arquivo.readlines()
texto.append(input(contador(i, f,p)))
arquivo = open(input(contador(i, f,p)), 'w')
arquivo.writelines(texto)
arquivo.close()
contador(i, f,p)
First of all, you make your function with print only, but no return.首先,您仅使用打印功能创建功能,但没有返回。 It means, it printing results, but it not put the result inside texto.append = ....
这意味着,它会打印结果,但不会将结果放入 texto.append = ....
texto.append = input are wrong. texto.append = 输入错误。 texto.append(str) are correct.
texto.append(str) 是正确的。
open(input... also wrong. correct: variable=open(filename,'w') open(input...也错了。正确:variable=open(filename,'w')
example:例子:
cont = 0
def contador(i, f, p):
global temp
temp.append(f'A contagem de {i} até {f} de {p} em {p}')
temp.append('\n\n000=>')
if i < f:
cont = i
numeros_contados=''
while cont <= f:
numeros_contados = numeros_contados+f'{cont},'
cont += p
temp.append(numeros_contados)
temp.append('\nFIM!\n')
else:
cont = i
numeros_contados = ''
while cont >= f:
numeros_contados = numeros_contados+f'{cont},'
cont -= p
temp.append(numeros_contados)
temp.append('\nFIM!\n')
return
print('Agora é sua vez, personalize um contador:')
i = int(input('Inicio: '))
f = int(input('Fim: '))
p = int(input('Passo: '))
nomarq = input('Nome do arquivo a ser editado:')
arquivo = open(nomarq, 'r')
texto = arquivo.readlines()
temp = texto
contador(i, f, p)
arquivo = open(nomarq, 'w')
for a in temp:
arquivo.write(a)
arquivo.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.