简体   繁体   English

Python:如何从字符串的开头删除空引号和新行?

[英]Python: How to remove empty quotes and a new line from the beginning of the string?

Below is the string I'm operating on.下面是我正在操作的字符串。

'exampleURL': ' '
                               'https://someurl.com'

I'm trying to clean it up using the code below.我正在尝试使用下面的代码清理它。 But for some reason, it doesn't work.但由于某种原因,它不起作用。

value_to_keep = (value_to_keep.rstrip('"').lstrip('"').rstrip("\n").lstrip("\n").rstrip("'").lstrip("'"))

I want the desired output to be我想要想要的输出

'exampleURL': 'https://someurl.com'

What is it that I'm missing here?我在这里错过了什么?

Answering my own question.回答我自己的问题。 I have solved the issue by improving the pattern.我已经通过改进模式解决了这个问题。

rstrip("' '").lstrip("' '")

Instead of one single quote, I have added two with a space.我用空格添加了两个,而不是一个单引号。 Because, that's how it is in the string.因为,这就是它在字符串中的方式。

You can simply use the replace method and replace(" ", "")您可以简单地使用 replace 方法和 replace(" ", "")

    foo = {
           'exampleURL': '                           
       https://someurl.com'.replace(" ", "")
    }

print(foo['exampleURL'])

Output: https://someurl.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM