简体   繁体   中英

Python will not write to a CSV file and there are no errors; the print function works

I have 3 lists of integers. They are then converted into strings. When trying to write them to the CSV file, the file itself is generated, but completely empty. This has stumped all Python users in my lab.

I have tried isolating the different lists and writing them to CSVs. They all return blank.

The print function works.

LP = [25,26,27,28,30,67,71,9,17,18,3,4,46,47,48,49, 55, 56, 65, 66] #(190)

RP = [92,93,98,107,114,115,116,156,117,160,119,144,145,135,136,137,138,155,106,
      154] #(190)
RF = [90,94,95,96,97,101,102,103,104,105,106,113,118,120,121,129,128,134,141,
      142,143,157,158,151,152,153,159,136,137,138,155,92,93,107] #(561)

LP = list(map(str, LP))
RP = list(map(str, RP))
RF = list(map(str, RF))

f = open("All Connections Between Nodes.csv", "w")


def run(lobe):
    seperator = ","
    for num in lobe:
        for num2 in lobe:
            if lobe.index(num2) <= lobe.index(num):
                pass
            elif lobe.index(num) == lobe.index(num2):
                pass

            elif lobe == LP:
                f.write("" + (num) + seperator + (num2) + "")
                f.write("\n")
                print(num, num2)
            elif lobe == RP:
                f.write("" + (num) + seperator + (num2) + "")
                f.write("\n")
                print("" + (num) + seperator + (num2) + "")
            elif lobe == RF:
                f.write("" + (num) + seperator + (num2) + "")
                f.write("\n")
                print("" + (num) + seperator + (num2) + "")


if __name__ == "__main__":

    run(LP)
    run(RP)
    run(RF)

The CSV file should look like this:

25 26
25 27
25 28

And so on. Each pair should only exist once. (eg, if 25-26 exists, there should not be a 26-25)

I think the scope is not correct with open the file:

LP = [25,26,27,28,30,67,71,9,17,18,3,4,46,47,48,49, 55, 56, 65, 66] #(190)

RP = [92,93,98,107,114,115,116,156,117,160,119,144,145,135,136,137,138,155,106,
      154] #(190)
RF = [90,94,95,96,97,101,102,103,104,105,106,113,118,120,121,129,128,134,141,
      142,143,157,158,151,152,153,159,136,137,138,155,92,93,107] #(561)

LP = list(map(str, LP))
RP = list(map(str, RP))
RF = list(map(str, RF))

seperator = ','

def run(lobe):
    with open("All Connections Between Nodes.csv", 'w') as f:
        for num in lobe:
            for num2 in lobe:
                if lobe.index(num2) <= lobe.index(num):
                    pass
                elif lobe.index(num) == lobe.index(num2):
                    pass

                elif lobe == LP:
                    f.write(''+(num)+seperator+(num2)+'')
                    f.write('\n')
                    print(num,num2)
                elif lobe == RP:
                    f.write(''+(num)+seperator+(num2)+'')
                    f.write('\n')
                    print(''+(num)+seperator+(num2)+'')
                elif lobe == RF:
                    f.write(''+(num)+seperator+(num2)+'')
                    f.write('\n')
                    print(''+(num)+seperator+(num2)+'')   




if __name__ == '__main__':


    run(LP)
    run(RP)
    run(RF)

Add f.flush() as in:

if __name__ == "__main__":
    run(LP)
    run(RP)
    run(RF)
    f.flush()

or, set the file to be unbuffered when you create the object:

f = open("All Connections Between Nodes.csv", "w", 0)

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