【问题标题】:Python - Write for loop output to file [duplicate]Python - 将for循环输出写入文件[重复]
【发布时间】:2017-08-26 03:24:01
【问题描述】:

我有一个输出 IP 列表的函数。

def convertHostNamesToIps(hostnames):
    ip = server.system.search.hostname(sessionKey, hostnames )
    for ips in ip:
        print (ips.get('ip'))

输出通常看起来像 以下是 CSDP_LAB_STAGING 的 IP

172.29.219.123
172.29.225.5
172.29.240.174
172.29.225.46
172.29.240.171
172.29.240.175
172.29.219.119
172.29.219.117
172.29.219.33
172.29.219.35
.
.
.
172.29.219.40
172.29.219.35
172.29.219.40
172.29.219.118
172.29.219.121
172.29.219.115
172.29.225.51

现在我想将此输出写入文件。

我所做的是

def convertHostNamesToIps(hostnames):
    ip = server.system.search.hostname(sessionKey, hostnames )
    sys.stdout=open("test.txt","w")
    for ips in ip:
        print (ips.get('ip'))
    sys.stdout.close()

但上面的代码只将最后一个 IP 写入test.txt。我以为我可能会弄乱缩进,但这对我有帮助。还有什么我想念的吗?

附:这是我的第一个 python 脚本,如果我做了一些非常愚蠢的事情,请原谅我。

【问题讨论】:

  • 这太复杂了。请阅读有关open 函数和一般文件对象的Python 文档(尤其是write 方法)。
  • @ForceBru 说了什么。这应该通过open 自己完成。但是,如果您想忽略所有警告,其他人都会给您直接弄乱 sys.stdout ..... 只需将 sys.stdout.flush() 放在 sys.stdout.close() 的正上方

标签: python


【解决方案1】:
def convertHostNamesToIps(hostnames):
ip = server.system.search.hostname(sessionKey, hostnames )
iplist = [] # Make a temporal list.
for ips in ip:
    print (ips.get('ip')) # This only print your ips, it isn't necesary.
    iplist.append(ips.get('ip')) # Add the current ip in the list.
with open("test.txt","w") as txt: # Open the file but when you finish to use the file it will automatically close.
    txt.writelines(iplist)

希望对你有帮助。

【讨论】:

    【解决方案2】:

    我什至不知道最后一个 IP 是如何保存的,因为您的函数中没有任何 write。 你可以试试这个:

    def convertHostNamesToIps(hostnames):
        ip = server.system.search.hostname(sessionKey, hostnames )
        list_ips = str ()
        for ips in ip:
        list_ips = list_ips + ips.get('ip') + '\n'
    
    with open ('test.txt', 'w') as file:
        file.write (list_ips)
    

    你需要像file.write () 这样的东西来保存你的ips。我将所有 ips 放在一个字符串中,以便它们更易于保存在文件中。 with 块不需要任何 close 函数

    编辑(我无法评论) 这两种方式的区别:

    my_file = open ('test.txt', 'w')
    

    my_file = open ('test.txt', 'a')
    

    只是第一个,在函数调用执行之前文件中的所有内容都将被删除。使用附加,它不会,my_file.write(something_to_add) 将被添加到文件的末尾。 但是在'w' 模式下打开将在执行此精确行时删除文件 我测试了自己,这适用于 'w' 以及 'a'

    【讨论】:

      【解决方案3】:

      我浏览了上面每个人的回复,并尝试了每一个。但是每个解决方案都只导致最后一个 IP 被打印到文件中。阅读documentation 让我得出结论,我需要追加到文件而不是写入文件。

      def convertHostNamesToIps(hostnames):
          ip = server.system.search.hostname(sessionKey, hostnames )
          my_file=open("test.txt","a")
          for ips in ip:
              my_file.write(ips.get('ip')+'\n')
          my_file.close() 
      

      【讨论】:

      • 那意味着你的函数被重复调用,最后一次调用的IP列表只包含一个IP。但是没有办法从你的问题中推断出来。
      【解决方案4】:

      重新分配sys.stdout?那是……勇敢。

      您可以将打开的文件分配给其他变量,然后调用其write 方法。如果您想在不同的行中添加内容,则必须自己添加。

      def convertHostNamesToIps(hostnames):
          ip = server.system.search.hostname(sessionKey, hostnames )
          my_file=open("test.txt","w")
          for ips in ip:
              my_file.write(ips.get('ip')+'\n')
          my_file.close()
      

      【讨论】:

        猜你喜欢
        • 2020-08-08
        • 2014-07-29
        • 1970-01-01
        • 2019-07-20
        • 1970-01-01
        • 1970-01-01
        • 2015-10-16
        • 2018-01-12
        • 1970-01-01
        相关资源
        最近更新 更多