"IP 주소"라는 열이있는 csv 문서와 그 안에 임의의 데이터가있는 세 개의 다른 열이 있습니다. 내 목표는 IP 주소 열을 반복하고 각 IP 주소에서 bulkfunc를 실행하여 내용을 텍스트 파일로 출력하는 것입니다.
데이터에 올바르게 액세스하는 팬더가 있지만 어떤 이유로 두 번 반복되므로 CSV에 3 개의 IP 주소가 있으므로 6 개의 출력 파일을 얻습니다.
def bulkcsv():
df = pd.read_csv(('csvfiles/' + inputfile), dtype=str, usecols=['IP Address'])
#for row in df:
df.applymap(bulkfunc)
내 bulkfunc 함수는 다음과 같습니다.
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()
csv는 다음과 같습니다.
IP Address random crap hiya intwer
10.90.11.252 dawd4 4654 14512
10.90.11.253 144 0
10.90.11.254 203
이것을 시도하면 문제가 해결됩니다.
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이 둘 이상의 차원 DataFrame에서 잘 작동한다고 믿습니다. 그렇지 않으면 시리즈로 취급되어야합니다.
나는 이것이 아마도 Pandas에 대한 버그 또는 변경 요청 일 수 있다고 생각합니다. 그 경로를 시도해 볼 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다