繁体   English   中英

Python不会写入CSV文件,并且没有错误; 打印功能有效

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

我有3个整数列表。 然后将它们转换为字符串。 尝试将它们写入CSV文件时,将生成文件本身,但完全为空。 这困扰了我实验室中的所有Python用户。

我尝试隔离不同的列表并将其写入CSV。 他们都返回空白。

打印功能有效。

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)

CSV文件应如下所示:

25 26
25 27
25 28

等等。 每对只能存在一次。 (例如,如果存在25-26,则不应有26-25)

我认为打开文件的范围不正确:

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)

添加f.flush()如下:

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

或者,在创建对象时将文件设置为不缓冲:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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