[英]How to remove quotes from start and end of the string at a specific line in python?
[英]Python: How to remove empty quotes and a new line from the beginning of the string?
下面是我正在操作的字符串。
'exampleURL': ' '
'https://someurl.com'
我正在尝试使用下面的代码清理它。 但由于某种原因,它不起作用。
value_to_keep = (value_to_keep.rstrip('"').lstrip('"').rstrip("\n").lstrip("\n").rstrip("'").lstrip("'"))
我想要想要的输出
'exampleURL': 'https://someurl.com'
我在这里错过了什么?
回答我自己的问题。 我已经通过改进模式解决了这个问题。
rstrip("' '").lstrip("' '")
我用空格添加了两个,而不是一个单引号。 因为,这就是它在字符串中的方式。
您可以简单地使用 replace 方法和 replace(" ", "")
foo = {
'exampleURL': '
https://someurl.com'.replace(" ", "")
}
print(foo['exampleURL'])
Output: https://someurl.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.