我有一段代码可以编写这样的 txt 文件:
library(shiny)
library(shinyFeedback)
library(tidyverse)
ui <- fluidPage(
useShinyFeedback(),
textInput("lst", "Enter numbers in a comma-delimited list"),
textOutput("lst")
)
server <- function(input, output, session) {
lst <- reactive({
bad_vals <- input$lst %>%
str_split(",") %>%
unlist() %>%
keep(~ .x %>% as.numeric() %>% is.na()) %>%
suppressWarnings()
err_msg <- str_interp(
"These aren't numbers: ${str_c(bad_vals, collapse = ', ')}"
)
print(err_msg)
feedbackDanger("lst", !is_empty(bad_vals), err_msg)
req(is_empty(bad_vals))
input$lst
})
output$lst <- renderText(lst())
}
shinyApp(ui, server)
但我想要这样的东西:
f2.csv,val,2
f2.csv,val,5
f2.csv,new,234
f2.csv,new,432
f2.csv,old,3
f2.csv,old,437
f2.csv,val,2
f2.csv,val,9
我的代码:
f2.csv,val,new,old
f2.csv,2,234,3
f2.csv,5,432,437
f2.csv,2,NaN,NaN
f2.csv,9,NaN,NaN
答案 0 :(得分:1)
Pandas 有一个内置的编写器:
df.to_csv(open("f2.csv","w"), header=True)