Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中替换字符串中的单双引号_Python_String_Replace_Double Quotes - Fatal编程技术网

在python中替换字符串中的单双引号

在python中替换字符串中的单双引号,python,string,replace,double-quotes,Python,String,Replace,Double Quotes,给定字符串为: str1= "sentence1 and sentence2", "sentence3 and sentence4", "sentence5 and sentence6" 期望输出为: sentence1 and sentence2; sentence3 and sentence4; sentence5 and sen

给定字符串为:

str1=
     "sentence1 and  
           sentence2",
     "sentence3 and 
           sentence4",
     "sentence5 and
           sentence6"
期望输出为:

sentence1 and  
         sentence2;
sentence3 and 
         sentence4;
sentence5 and
         sentence6;
我现在使用的已编辑Python代码:

它给我的数据如下,发现作品精细和替换,与;也很好,但现在我想要 要摆脱单双引号,如下所示 类似于replace_string=replace_string.replace“”,;不是移除那些单双 每个句子开头的引号

第1句和 句子2; 第3句和 句子4; 第5句和
如果字符串是以段落格式分配的,则为第6句。然后建议使用三重引号。像这样

str1 = """
"sentence1 and  
      sentence2",
"sentence3 and 
      sentence4",
"sentence5 and
      sentence6"
"""
然后使用

str1 = str1.replace(r'",', r";") # this replaces ", with ;
str1 = str1.replace(r'"', '') # this replaces " with nothing.
print str1
关于另一个查询:

str1.find("",")!=-1 doesnt work to find ",
它将不起作用,因为它将两个参数视为传递给find函数的参数,一个是空字符串,另一个只是打开双引号,没有结束双引号

在这种情况下,您可以通过使用r';'来使用原始字符串或者像这样使用转义字符\

replace方法不会在同一个字符串中进行替换,字符串是不可变的,因此它会创建一个新字符串,而该字符串没有分配给变量,因此会丢失。转换也是多余的-您已经有了一个字符串。 只是

尽量不要将Python对象的名称(如list)用作变量名,这是个麻烦的问题

编辑: 至于find不起作用——按照您编写它的方式,您应该得到一个例外。为了使这些东西值得错过,您可以使用Python str对象而不是str1!如果要在带引号的字符串中使用双引号,可以:

用反斜杠逃避它

str1.find\=-一,

只需对字符串使用单引号

str1.查找','=-一,

这个怎么样

str1 = str1.replace('",', ';')

你错过了作业。

类似于替换\,;你必须先替换“,”和“;”然后替换“”,非常感谢您指出我已经编辑了代码..我的find方法不适用于..如果您能帮助我的话,谢谢..我已经编辑了问题..现在可以查找了,但我得到的输出与上面问题中编辑后的一样
for idx, line in enumerate(my_list):
    my_list[idx] = line.replace('",', ';'.replace('"', '')
str1 = str1.replace('",', ';')