如何在python中删除和添加字符/整数?

时间:2015-11-27 06:26:01

标签: python regex python-2.7

8/3/2015,1,F&B,L,>50 yrs,> 50 mio,100-<500rb,085800000129

实际上我想做的是预处理这个输入,但我不知道如何处理它。问题是我想删除05_,08_,02_并使用python语言在000129前面添加像“0858”这样的新整数。有什么指导可以解决吗?

这是所需的输出:

library(shiny)
shinyUI(fluidPage(
  titlePanel("submitButton example"),
  fluidRow(
    column(3, wellPanel(
      sliderInput("n", "N:", min = 10, max = 1000, value = 200,
                  step = 10),
      checkboxInput("checkbox_1", label = "Message", value = FALSE),
     uiOutput('test')

      ,actionButton("Submit",label ="Submit" )
    )),
    column(6,
           plotOutput("plot1", width = 400, height = 300),
           verbatimTextOutput("text")
    )
  )
))

1 个答案:

答案 0 :(得分:0)

在最后一个逗号后添加0858

data = "8/3/2015,1,F&B,L,05_>50 yrs,08_> 50 mio,02_100-<500rb,000129"

last = data.rfind(",")
if last > -1:
    data = data[:last+1] + "0858" + data[last+1:]

之前寻找_并删除两个字符
while True:
    pos = data.find('_')
    if pos < 0:
        break
    data = data[:pos-2] + data[pos+1:]

可以做得更好。我留给你:)