Python保存到csv中的新行

时间:2019-02-21 15:08:49

标签: python csv ip

我尝试创建一个通过一些IP地址运行的scipt,并对其进行ping操作。 我有一个问题,当我保存到csv时,它会写入所有ip,并且在长行中的状态为1

我希望将它们放在单独的行中。我不知道该怎么做

有人可以帮助我吗?

hostnames = 
['10.245.0.100','10.245.0.800','google.com','10.45.80.21','10.33.33.21'] 
#example

#mylist = 0
#liste = 0

#for i in hostname:
#    value = os.system("ping -c 1" + i)
#    print("ip er ", i)
#    print(value)
#    if value == "1":
#        print("skrammel")
# 
#print("værdien er", i)



import os
import csv

liste2 = []

for hostname in hostnames:
    response = os.system('ping -c 1 ' + hostname)
if response == 0:
    print (hostname, 'is up')
elif response == 1:
    print (hostname, 'is down')
    with open('bruger.csv', 'a', newline ="\n") as fd:
        fd.write(hostname+",")


print ("Første kørsel gennemført")

with open('bruger.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
    response2 = os.system('ping -c 1 ' + hostname)
    if response == 0:
        print(hostname, 'is up')
    elif response == 1:
        print(hostname, 'is down')
    print(f'Processed {line_count} lines.')

0 个答案:

没有答案