如何打印'%d'和& python中的一个类中的'%s'

时间:2016-05-31 12:57:32

标签: python python-2.7 class oop beautifulsoup

我正在写一个脚本,它会计算一些特定的单词并给出具体的单词计数。

我目前只是在课堂上打印数据。

我的下一个任务是使用数据驱动框架将这些值放在excel文件中。

这是我到目前为止所做的事情:

a = driver.page_source
soup = BeautifulSoup(a, "html.parser")


class counter_class:
    def count(self, tittle, block_code):
        blockcode_passed = block_code.count("Passed")
        blockcode_blocked = block_code.count("Blocked")
        blockcode_fail = block_code.count("Failed")
        blockcode_retest = block_code.count("Retest")
        blockcode_cannot_test = block_code.count("Connot Test")
        blockcode_completed = block_code.count("Completed")
        blockcode_passwc = block_code.count("Pass With Concern")
        blockcode_untested = block_code.count("Untested")

        print '%s' + ' ' + '%d' %(tittle,blockcode_passed)
        print '%s' + ' ' + '%d' %(tittle,blockcode_fail)
        print "Apps Gateway(Untested)" + ' ' + '%d' %(blockcode_untested)
        print "Apps Gateway(Blocked)" + ' ' + '%d' %(blockcode_blocked)
        print "Apps Gateway(Retest)" + ' ' + '%d' %(blockcode_retest)
        print "Apps Gateway(Cannot Test)" + ' ' + '%d' %(blockcode_cannot_test)
        print "Apps Gateway(Completed)" + ' ' + '%d' %(blockcode_completed)
        print "Apps Gateway(Pass With Concern)" + ' ' + '%d' %(blockcode_passwc)




apps_gateway = soup.find_all("div", {"id":"group-3191427"})
apps_gateway_str = str(apps_gateway)
apps_gateway_obj=counter_class()
apps_gateway_obj.count("appsgateway",apps_gateway_str)

代码的第二部分可以使用但代码的第一部分:

print '%s' + ' ' + '%d' %(tittle,blockcode_passed)
print '%s' + ' ' + '%d' %(tittle,blockcode_fail)

给我错误:

print '%s' + ' ' + '%d' %(tittle,blockcode_passed)
TypeError: %d format: a number is required, not str

4 个答案:

答案 0 :(得分:1)

操作顺序存在问题。实际执行的是:print '%s' + ' ' + ('%d' %(tittle,blockcode_passed))

Python正在尝试使用tittle代替%d参数。您可以使用以下任一项进行更改:

print ('%s' + ' ' + '%d') %(tittle,blockcode_passed)
# or
print "%s %d" %(tittle,blockcode_passed)

答案 1 :(得分:0)

您将for item in array { print(item.dynamicType) } 运算符的格式应用于最后一个字符串,在本例中为%。这不适用于'%d'参数。

答案 2 :(得分:0)

您可以从格式字符串中删除字符串连接开始:file <- "C:/Users/akrun/Misc/Data_to_analyze.csv" data1 <- fread(file) dt1 <- data1[tp==0] dt2 <- data1[tp!=0] res <- dt2[dt1, on = c("protein", "fract"), allow.cartesian = TRUE ][, if(.N >1) { .(r1= V1, r2 = i.V1, time0 = i.tp,Othertime = tp, pval = t.test(abundance, i.abundance)$p.value) } # else .(r1=V1, r2 = i.V1, time0 = i.tp, Othertime = tp, pval = NA_real_) , by = .(protein, fract)] res1 <- melt(res, measure = patterns("^r\\d+", "time"), value.name= c("V1", "time"))[, variable := NULL][] res2 <- unique(res1, by = c("protein", "fract", "V1")) res3 <- data1[res2, on = "V1"][order(as.numeric(V1))][, c('i.protein', 'i.fract') := NULL][] head(res3) # V1 protein line tp fract abundance pval time #1: 1 PCP000002 n 0 1 46697305.3 0.515626208527 1 #2: 2 PCP000007 n 0 1 8384565.8 0.000643157099 1 #3: 3 PCP000008 n 0 1 570026.3 0.000006871077 1 #4: 4 PCP000012 n 0 1 1018257.3 0.085610886030 1 #5: 5 PCP000017 n 0 1 157877521.4 0.016528856828 1 #6: 6 PCP000018 n 0 1 426215586.5 0.046130079694 1 - &gt; '%s' + ' ' + '%d'。您的语法问题是在连接发生之前首先完成字符串格式化,因此在您的情况下:

'%s %d'

python尝试用你给它的元组的第一个元素替换'%s' + ' ' + '%d' % ('s', 2) ,这是一个字符串,给你错误%d

答案 3 :(得分:0)

注意运算符优先级

'%s' + ' ' + '%d' %(tittle,blockcode_passed)

装置

'%s' + ' ' + ('%d' %(tittle,blockcode_passed))