当我尝试从命令行执行python程序时,它会出现以下错误。这些错误不会对我的输出造成任何问题。我不希望它显示在命令行
中Traceback (most recent call last):
File "test.py", line 88, in <module>
p.feed(ht)
File "/usr/lib/python2.5/HTMLParser.py", line 108, in feed
self.goahead(0)
File "/usr/lib/python2.5/HTMLParser.py", line 148, in goahead
k = self.parse_starttag(i)
File "/usr/lib/python2.5/HTMLParser.py", line 226, in parse_starttag
endpos = self.check_for_whole_start_tag(i)
File "/usr/lib/python2.5/HTMLParser.py", line 301, in check_for_whole_start_tag
self.error("malformed start tag")
File "/usr/lib/python2.5/HTMLParser.py", line 115, in error
raise HTMLParseError(message, self.getpos())
HTMLParser.HTMLParseError: malformed start tag, at line 319, column 25
我怎样才能抑制错误?
答案 0 :(得分:6)
将stderr重定向到/dev/null
。
python somescript.py 2> /dev/null
答案 1 :(得分:5)
不捕捉HTMLParseError对你有用吗?如果test.py
是你的python文件的名称,它会传播到那里,所以它应该。
以下是如何抑制此类错误的示例。您可能需要调整一下以匹配您的代码。
try:
# Put parsing code here
except HTMLParseError:
pass
您也可以通过将stderr重定向为null来抑制错误消息,就像Ignacio建议的那样。要在代码中执行此操作,您只需编写以下内容:
import sys
class DevNull:
def write(self, msg):
pass
sys.stderr = DevNull()
但是,这可能不是您想要的,因为从您的错误看,脚本执行已停止,您可能希望继续执行。
答案 2 :(得分:3)
在python 3中,@ Boo Yaniv的答案可以简化为
sys.stderr = object
因为python3中的每个类都是从Object
继承的,所以从技术上来说这是可行的,至少我在python 3.6.5环境中自己尝试过。
答案 3 :(得分:0)
这是一种更易读,简洁的解决方案,用于处理可以忽略的错误,而不必求助于典型的try / except / pass代码块。
library(shiny)
library(rhandsontable)
library(dplyr)
library(shinydashboard)
ui <- fluidPage( fluidRow(column(6, uiOutput("selA"))),
fluidRow(column(6, rHandsontableOutput('tbl1'))
)
)
server <- function(input, output, session){
dt0 <- data.frame( A = c("S2","S2","S2","S4","S4","S4"),
B = c("1","2","3","1","2","3"),
C = c(10,20,30,40,15,25),
D = c("A","B","C","D","E","F"))
# get the data for the selected BA
dt <- reactive(subset(dt0, A %in% input$selA))
# Render selectInput selBA
output$selA <- renderUI({
ba <- as.vector( unique(dt0$A) )
selectInput("selA","Choose BA", choices = ba)
})
DF <- data.frame("X" = c(""),
"Y" = c(""),
"Z" = c(""),
"Type_action" = c(""))
values <- reactiveValues(data = DF)
Y <- reactiveVal()
Z <- reactiveVal()
observe({
if(!is.null(input$tbl1)){
values$data <- as.data.frame(hot_to_r(req(input$tbl1)))
}
})
observeEvent(input$tbl1,{
Y(hot_to_r(input$tbl1)$Y)},
ignoreInit= TRUE
)
observeEvent(input$tbl1,{
Z(hot_to_r(input$tbl1)$Z)},
ignoreInit= TRUE
)
output$tbl1 = renderRHandsontable({
req(input$selA)
tmpTable <- rhandsontable(values$data, rowHeaders = FALSE, selectCallback = TRUE, width =
1000, height = 500) %>%
hot_table(highlightCol = TRUE, highlightRow = TRUE, stretchH = "all") %>%
hot_col(col = "X", type = "dropdown", colWidths = 90, source =
sort(unique(dt()$B))) %>%
hot_col(col = "Y", type = "dropdown", colWidths = 65, source =
sort(unique(dt()$D))) %>%
hot_col(col = "Z", type = "dropdown", colWidths = 60,source =
sort(unique(dt()$D))) %>%
hot_col(col = "Type_action", colWidths = 50, readOnly = TRUE, type = "text")
if(!is.null(input$tbl1_select$select$r) && !is.na(values$data$Y[input$tbl1_select$select$r])
&& !is.na(values$data$Z[input$tbl1_select$select$r])){
values$data$Type_action <- ifelse(match(Y(), LETTERS) < match(Z(), LETTERS),"Upgrade","Downgrade")
}
tmpTable
})
}
shinyApp(ui, server)