How to delete a character from a string using Python

前端 未结 16 1571
耶瑟儿~
耶瑟儿~ 2020-11-22 16:35

There is a string, for example. EXAMPLE.

How can I remove the middle character, i.e., M from it? I don\'t need the code. I want to know:

16条回答
  •  长情又很酷
    2020-11-22 17:29

    If you want to delete/ignore characters in a string, and, for instance, you have this string,

    "[11:L:0]"

    from a web API response or something like that, like a CSV file, let's say you are using requests

    import requests
    udid = 123456
    url = 'http://webservices.yourserver.com/action/id-' + udid
    s = requests.Session()
    s.verify = False
    resp = s.get(url, stream=True)
    content = resp.content
    

    loop and get rid of unwanted chars:

    for line in resp.iter_lines():
      line = line.replace("[", "")
      line = line.replace("]", "")
      line = line.replace('"', "")
    

    Optional split, and you will be able to read values individually:

    listofvalues = line.split(':')
    

    Now accessing each value is easier:

    print listofvalues[0]
    print listofvalues[1]
    print listofvalues[2]
    

    This will print

    11

    L

    0

提交回复
热议问题