简体   繁体   中英

Writting multiple times in a file in a loop using python

I'm writing large matrices in python, like (2^20, 2^20). To deal with that my approach is to write each element in a file with its respective row and column.

I've tried to resolve it in like that:

l = 20
j = 1
delt = -1
for x in range(0,2**l):
    for y in range(0,l):
        k = (y+1)%l
        if check_nth_bit(x,y) == 0:
            a = ([x,x,-j*h/2])
            with open("file.txt", "w") as f:
                f.write(str(a))
        else:
            b = ([x,x,j*h/2])
            with open("file.txt", "w") as f:
                f.write(str(b))

The way I've done, only the last element is written in the file. Can anyone help me?

Each time you use with open("file.txt", "w") as f: you're opening the file in write mode - specifically, in overwrite mode. Every value is being written to the file, but each time you loop, you erase the file and start writing it anew.

You can avoid this by opening in append mode as with open("file.txt", "a") as f: but opening and closing files over and over doesn't make much sense (and it's computationally very expensive; your program will get very slow.), Why not move that logic outside of your loops? so you only have to open the file once?

l = 20
j = 1
delt = -1
with open ("file.txt", "w") as f:
    for x in range(0,2**l):
        for y in range(0,l):
            k = (y+1)%l
            if check_nth_bit(x,y) == 0:
                a = ([x,x,-j*h/2])-
                f.write(str(a))
            else:
                b = ([x,x,j*h/2])-
                f.write(str(b))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM