我有一个csv文档,其中包含一个名为" IP Address"和其他三个包含随机数据的列。我的目标是遍历IP地址列并在每个IP地址上运行bulkfunc,将内容输出到文本文件。
我让熊猫正确访问数据,但由于某种原因,它会循环两次,因此,由于我在CSV中有3个IP地址,因此我得到6个输出文件。
rstn
这是我的bulkfunc函数:
def bulkcsv():
df = pd.read_csv(('csvfiles/' + inputfile), dtype=str, usecols=['IP Address'])
#for row in df:
df.applymap(bulkfunc)
继承人csv:
def bulkfunc(x):
global f
global ip
ip = x
f = open('results/%s' % ip + "_" + datetime.now().strftime("%Y-%m-%d@%H%M") + '.txt', 'a')
print "Static Information: "
f.write("Static Information: ")
print "-" * 30
f.write("-" * 30)
parsenetworkcsv(ip)
ping(ip)
nmaprun(ip, "-sV")
print "The output is complete."
f.write("-" * 30)
f.write("created by OP")
f.close()
答案 0 :(得分:2)
试试这个,这应该可以解决问题。
import pandas as pd
import time
from datetime import datetime
def bulkcsv():
inputfile = 'inp.csv'
df = pd.read_csv(('csvfiles/' + inputfile), dtype=str, usecols=['IP Address'])
#print df
#for row in df:
#print df['IP Address'].shape
df['IP Address'].map(bulkfunc)
def bulkfunc(x):
global f
global ip
ip = x
f = open('results/%s' % ip + "_" + datetime.now().strftime("%Y-%m-%d@%H%M%S") + '.txt', 'a')
print "Static Information: "
f.write("Static Information: ")
print "-" * 30
f.write("-" * 30)
parsenetworkcsv(ip)
ping(ip)
nmaprun(ip, "-sV")
print "The output is complete."
f.write("-" * 30)
f.write("created by OP")
f.close()
time.sleep(1)
bulkcsv()
<强>输出:强>
IP Address
0 10.90.11.252
1 10.90.11.253
2 10.90.11.254
(3L,)
Static Information:
------------------------------
The output is complete.
Static Information:
------------------------------
The output is complete.
Static Information:
------------------------------
The output is complete.
问题似乎是因为pd.read_csv。在您的代码中,您已将其作为DataFrame读取。它的形状是(3L,1),因为applymap循环两次(索引0和1)。但是,当我们使用系列时,由于您只有一列,因此地图可以为您完成工作。您还可以使用,应用DataFrame的功能。我相信applymap适用于超过1维的DataFrame,否则它应该被视为系列。
我相信这可能是Pandas的错误或更改请求。你可以尝试这条路线。