我在python中编写一个代码来计算文件中出现了多少类型错误代码
我的档案:
Tue Sep 01 23:50:17 2015|132491151|959796398631|95931002712|Tue Sep 1 23:50:17 2015|Tue Sep 1 23:50:17 2015|CMT|Undelivered|none|Submit|0|SMSC_no_error|SMPP|ESMEGW|16 Bit|39|78|no||no|no||None|No|NO|no|no|0|0|0|0||||0|0||0|no|no|default_billing|-1|0|no|no|1|1|0|0|1|1|0|0||78456|ussdt|78456|Wed Sep 02 23:50:17 2015|SR|mptcdma2_r|87690|IN|012463814411280170009|||0|1541843||0|0||0|||||||||||||||||||||0|
但我没有得到正确的输出。我失踪的地方。这是我的代码
#!/usr/bin/python
import sys
import xlsxwriter
import subprocess
import pprint
from addict import Dict
data = Dict()
if len(sys.argv) == 2:
date = sys.argv[1]
print date
else:
date = subprocess.check_output("date --date='01 hour ago' +%y%m%d%H",shell=True)
date=date.rstrip()
print date
input_dir = "/root/prac/Harini/CDR/"
result_dir = "/root/prac/Harini"
year = "20"+date[0:2]
month = date[2:4]
day = date[4:6]
hour = date[6:8]
db_date = year+"-"+month+"-"+day
array = ();
files = ();
try:
files = subprocess.check_output("ls " + input_dir + "SMSCDR_POSTPAID_" + date + "*.log", stderr=subprocess.STDOUT,shell=True)
array = files.splitlines()
except:
print "No files in the directory"
for single in array:
fp=open(single,"r")
string = fp.readlines();
for line in string:
array = line.split('|')
exp = array[7]
system_id = array[55]
last_failure = array[11]
assign_short_code = array[54];
if (exp == "Expired" or exp == "Undelivered"):
data.db_date.hour.system_id.assign_short_code.last_failure = data[db_date][hour][system_id][assign_short_code].setdefault(last_failure, 0)+1;
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(data)
输出:
{ '2016-04-08': { '11': { 'ussdt': { '78456': { 'SMSC_no_error': 0}}}},
'db_date': { 'hour': { 'system_id': { 'assign_short_code': { 'last_failure': 1}}}}}
预期输出
{ '2016-04-08': { '11': { 'ussdt': { '78456': { 'SMSC_no_error': 1}}}},
答案 0 :(得分:1)
您正在获取db_date
密钥,因为您正在添加它:
data.db_date.hour.system_id.assign_short_code.last_failure = data[db_date][hour][system_id][assign_short_code].setdefault(last_failure, 0)+1;
# ^^^^^^^
您正在使用addict。在标准的dict表示法中,该行等同于:
data['db_date']['hour']['system_id']['assign_short_code']['last_failure'] = data[db_date][hour][system_id][assign_short_code].setdefault(last_failure, 0)+1;
# ^^^^^^^^^
我认为你打算写这个:
data[db_date][hour][system_id][assign_short_code][last_failure] = data[db_date][hour][system_id][assign_short_code].setdefault(last_failure, 0) + 1
也就是说,您希望使用 db_date
(和其他变量)的值,而不是文字字符串'db_value'
。
答案 1 :(得分:0)
此:
data.db_date.hour.system_id.assign_short_code.last_failure = data[db_date][hour][system_id][assign_short_code].setdefault(last_failure, 0)+1;
应该是
data[db_date][hour][system_id][assign_short_code].setdefault(last_failure, 0)
data[db_date][hour][system_id][assign_short_code] = data[db_date][hour][system_id][assign_short_code] + 1
注意:在python中没有分号