Python 数到100000并将其写入文件

Python 数到100000并将其写入文件,python,file,counter,writing,Python,File,Counter,Writing,我已经有一段时间没有使用Python了,但我决定今天创建一个程序来帮助我完成一些我正在尝试做的工作。我正在尝试创建一个程序,它在每个数字后面写上1-100000的符号,但在创建文件后似乎无法删除该文件,因此它显示如下:1 | 2 | 3 | 4 我的代码: a = 0 b = "|" while a < 100000: a += 1 # Same as a = a + 1 new = (a,b) f = open("export.txt","a") #opens

我已经有一段时间没有使用Python了,但我决定今天创建一个程序来帮助我完成一些我正在尝试做的工作。我正在尝试创建一个程序,它在每个数字后面写上1-100000的符号,但在创建文件后似乎无法删除该文件,因此它显示如下:1 | 2 | 3 | 4

我的代码:

a = 0
b = "|"
while a < 100000:
    a += 1 # Same as a = a + 1 
    new = (a,b)
    f = open("export.txt","a") #opens file with name of "export.txt"
    f.write(str(new))
f.close()


infile = "export.txt"
outfile = "newfile.txt"

delete_list = ["(","," "'"]
fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
    for word in delete_list:
        line = line.replace(word, "")
    fout.write(line)
fin.close()
fout.close()
a=0
b=“|”
而a<100000:
a+=1#与a=a+1相同
新=(a,b)
f=打开(“export.txt”,“a”)#打开名为“export.txt”的文件
f、 写入(str(新))
f、 关闭()
infle=“export.txt”
outfile=“newfile.txt”
delete_list=[“(“,”,“,””]
翅片=打开(填充)
fout=打开(输出文件,“w+”)
对于fin中的行:
对于删除列表中的单词:
行=行。替换(单词“”)
四、写(行)
财务结束()
fout.close()
export.txt:

a = 0
b = "|"
while a < 100000:
    a += 1 # Same as a = a + 1 
    new = (a,b)
    f = open("export.txt","a") #opens file with name of "export.txt"
    f.write(str(new))
f.close()


infile = "export.txt"
outfile = "newfile.txt"

delete_list = ["(","," "'"]
fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
    for word in delete_list:
        line = line.replace(word, "")
    fout.write(line)
fin.close()
fout.close()

newfile.txt:

a = 0
b = "|"
while a < 100000:
    a += 1 # Same as a = a + 1 
    new = (a,b)
    f = open("export.txt","a") #opens file with name of "export.txt"
    f.write(str(new))
f.close()


infile = "export.txt"
outfile = "newfile.txt"

delete_list = ["(","," "'"]
fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
    for word in delete_list:
        line = line.replace(word, "")
    fout.write(line)
fin.close()
fout.close()

看起来你做了很多不必要的工作

如果您想要的是一个编号为0-99999的文件,每个文件后面都有
,则可以执行以下操作:

delim = "|"
with open('export.txt', 'w') as f:
    for a in xrange(100):
        f.write("%d%s" % (a, delim))
with open('export.txt', 'r') as fi:
    with open('newfile.txt', 'w') as fo:
        for line in fi:
            for word in line.split('|'):
                print(word)
                fo.write(word)
我不确定第二个文件的用途是什么,但一般来说,要打开一个文件进行读取,再打开一个文件进行写入,可以执行以下操作:

delim = "|"
with open('export.txt', 'w') as f:
    for a in xrange(100):
        f.write("%d%s" % (a, delim))
with open('export.txt', 'r') as fi:
    with open('newfile.txt', 'w') as fo:
        for line in fi:
            for word in line.split('|'):
                print(word)
                fo.write(word)

请注意,原始文件中没有换行符,因此fi中的行的
实际上正在读取“export.txt”的全部内容--这可能会导致问题。

请尝试以下方法写入文件:

numbers = []
for x in range(1,100001):
    numbers.append(str(x))

f = open('export.txt', 'w')
f.write('|'.join(numbers))
f.close()

谢谢,但当我尝试上面的一个时,它说xrange没有定义,而当我使用下面的一个时,文件是空的。@Matt这意味着你在python 3上——只需将
xrange
更改为
range
——第二个块只是一个示例,说明了如何从一个文件读取和写入另一个文件——它根本没有任何作用eally.在这种情况下,它会读取从我的第一个块创建的文件,并在不同的行上打印整数(并将不带分隔符的整数写入新文件),所以现在最上面的一个只计算到99,但做得很好,谢谢!有什么方法可以让它完成所有这些吗?@Matt Sure只需将
range(100)
更改为
range即可(100000)
。注意答案底部的注释——第二个块立即读取整个文件。太棒了,谢谢!比我想做的要简单得多。:)