我是python的新手,我的代码有这样的错误,即扫描IP地址列表并仅显示恶意软件IP列表: 进口口 从datetime导入datetime,date,timedelta 导入子流程 导入pyjq 将pandas导入为pd
# Initializes the variables for the directories
HomeDir = "/Users/mani/Downloads/"
ScriptDir = HomeDir + "/panpython"
ResultDir = "/Users/mani/Desktop/result"
# Create the dates
ToDay = datetime.now().strftime('%Y%m%d')
# checkDATE = (date.today() - timedelta(1)).strfttime('%Y%m%d')
ResultFile = "Test"
CheckDATE = "2015-10-01"
NOWDATE = "2015-10-02"
secretkey = 'secret key'
progToRun = 'python ' + ScriptDir + '/bin/panafapi.py -K ' + secretkey + ' --samples -j -r "{\\"query\\":{\\"operator\\":\\"all\\",\\"children\\":[{\\"field\\":\\"alias.ip_address\\",\\"operator\\":\\"contains\\",\\"value\\":\\"' + ResultFile + '\\"},{\\"operator\\":\\"any\\",\\"children\\":[{\\"field\\":\\"sample.update_date\\",\\"operator\\":\\"is in the range\\",\\"value\\":[\\"' + CheckDATE + 'T00:00:00\\",\\"' + NOWDATE + 'T23:59:59\\"]},{\\"field\\":\\"sample.create_date\\",\\"operator\\":\\"is in the range\\",\\"value\\":[\\"' + CheckDATE + 'T00:00:00\\",\\"' + NOWDATE + 'T23:59:59\\"]},{\\"operator\\":\\"any\\",\\"children\\":[{\\"field\\":\\"sample.malware\\",\\"operator\\":\\"is\\",\\"value\\":1},{\\"field\\":\\"sample.malware\\",\\"operator\\":\\"is\\",\\"value\\":4}]}]}]},\\"scope\\":\\"global\\",\\"size\\":1,\\"from\\":0,\\"sort\\":{\\"create_date\\":{\\"order\\":\\"desc\\"}}}" > ' + ResultDir + 'srciplist-' + ToDay + '.json'
# Run the panafpi
subprocess.check_output(progToRun, shell=True)
# Using pyjq to filter
filteredResultData = pyjq.all('.hits[]._source | .create_date + "," + .sha256')
file_to_open=sys.argv[1]
df=pd.read_csv(file_to_open)
df.to_csv(ResultDir + "/srciplist-" + ToDay + ".csv", sep=',')
我是在命令行中执行此操作,然后扫描并显示此错误
python finalauto.py xyz.txt
samples_search: 200 OK 339 0%
.......
samples_results: 200 OK 100% hits=1 total=674415 time=0:08:57.082 "complete"
错误:
Traceback (most recent call last):
File "finalauto.py", line 36, in <module>
df=pd.read_csv(file_to_open)
File "/usr/local/lib/python3.6/site-packages/pandas/io/parsers.py", line 709, in parser_f
return _read(filepath_or_buffer, kwds)
File "/usr/local/lib/python3.6/site-packages/pandas/io/parsers.py", line 455, in _read
data = parser.read(nrows)
File "/usr/local/lib/python3.6/site-packages/pandas/io/parsers.py", line 1069, in read
ret = self._engine.read(nrows)
File "/usr/local/lib/python3.6/site-packages/pandas/io/parsers.py", line 1839, in read
data = self._reader.read(nrows)
File "pandas/_libs/parsers.pyx", line 902, in pandas._libs.parsers.TextReader.read
File "pandas/_libs/parsers.pyx", line 924, in pandas._libs.parsers.TextReader._read_low_memory
File "pandas/_libs/parsers.pyx", line 978, in pandas._libs.parsers.TextReader._read_rows
File "pandas/_libs/parsers.pyx", line 965, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas/_libs/parsers.pyx", line 2208, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 778, saw 3
答案 0 :(得分:0)
您可以使用sys.argv[i]
从索引 i 的commad行解析参数
试试这个:
import sys
file_to_open=sys.argv[1] # 0 is the index of name of python prog, 1 is the index of first argument in command line.
df=pd.read_csv(file_to_open,sep=',')
#do whatever you want with the file
pd.to_csv()
用于将pandas数据帧保存为csv文件
如果您在命令行中传递python finalauto.py xyz.txt
,sys.argv[0]
将为您提供 finalauto.py ,sys.argv[1]
将为您提供 xyz.txt < / em>的
答案 1 :(得分:0)
1)标准库中有一个名为CSV的模块。在创建CSV时使用它可能更好。像这样使用:
import csv
with open("file.csv", 'w') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(ResultDir + "/srciplist-" + ToDay + ".csv")
2)以下是在命令行中打开文件的一些代码:
import sys
with open(sys.argv[1], 'r') as f:
contents = f.read()
# Continue code below