Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.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_Html_Python 3.x - Fatal编程技术网

如何替换python中包含引号的字符串?

如何替换python中包含引号的字符串?,python,html,python-3.x,Python,Html,Python 3.x,我有一个CSV文件是关于HTML的代码。 将熊猫作为pd导入 将numpy作为np导入 导入csv 导入seaborn作为sns 进口稀土 导入操作系统 pd.set_选项(“display.max_行”,100000000) pd.set_选项(“display.max_columns”,100000000) dirs=os.listdir('DataCollectionCA/')) 对于i,在dirs中: 如果os.path.splitext(i)[1]==“.csv”: 印刷品(一) 目

我有一个CSV文件是关于HTML的代码。

将熊猫作为pd导入
将numpy作为np导入
导入csv
导入seaborn作为sns
进口稀土
导入操作系统
pd.set_选项(“display.max_行”,100000000)
pd.set_选项(“display.max_columns”,100000000)
dirs=os.listdir('DataCollectionCA/'))
对于i,在dirs中:
如果os.path.splitext(i)[1]==“.csv”:
印刷品(一)
目录='DataCollectionCA/'
打印(“”)
df=pd.read_csv(目录+7197409.csv)#導入資料
df_num=len(df)#計算有多少行
打印(df_num)
实数=实数+1
将open('71974099999.csv','a',newline='',encoding=“utf-8”)作为csvfile:
writer=csv.writer(csvfile)
writer.writerow(['互動作者','發表時間','互動內容'])
对于范围内的post(1,实数):
将open(dirss+'7197409.csv',换行符='',encoding=“utf-8”)作为csvfile:
reader=csv.reader(csvfile)
column0=[读卡器中的行的行[0]
对于i,枚举中的行(第0列):
如果i==post:
row000=行
将open(dirss+'7197409.csv',换行符='',encoding=“utf-8”)作为csvfile:
reader=csv.reader(csvfile)
column1=[读卡器中的行的行[1]
对于j,枚举中的行(第1列):
如果j==post:
row001=行
将open(dirss+'7197409.csv',换行符='',encoding=“utf-8”)作为csvfile:
reader=csv.reader(csvfile)
column2=[读卡器中的行的第[2]行]
对于k,枚举中的行(第2列):
如果k==post:
row002=行
作者=row000
res_时间=第001行
原始\u html\u代码=行002
新的html代码\u 01=原始html代码。替换(“,”)
新建html代码02=新建html代码01。替换(“
”,“”) 打印(新的\u html\u代码\u 02) 打印(“=======”) 将open('71974099999.csv','a',newline='',encoding=“utf-8”)作为csvfile: writer=csv.writer(csvfile) writer.writerow([author,res\u time,new\u html\u code\u 02])
我想用Python替换以下字符串(它是HTML代码):

等等

我试图使用下面的代码来做这件事,但失败了。 我想换成空白

new_html_code_02 = re.sub('<div class=\"\"ContentGrid\"\">', " ", new_html_code_01)
new_html_code_02 = re.sub('<div class=""ContentGrid"">', " ", new_html_code_01)
new\u html\u code\u 02=re.sub(“”,”,new\u html\u code\u 01)
新的html代码02=re.sub(“”,“,新的html代码01)

新文件仍然显示这些字符串。我不知道要解决什么问题。

我不太确定您想要什么,但您尝试的第二个替换语句对我有效。您不需要逃避qoutes(
)。如果您只想替换静态表达式,甚至不需要使用正则表达式,您还可以使用Python的
replace()
方法的
string
类型:

import re


html = '<div class=""ContentGrid"">' \
       '<img data-icons="":~("" src=""' \
       '"" onload=""DrawImage(this)"" width=""300"" height=""617"">'

new_html = re.sub('<div class=""ContentGrid"">', " ", html)
print(new_html)

new_html = html.replace('<div class=""ContentGrid"">', " ")
print(new_html)
重新导入
html=“”\
''
new_html=re.sub(“”,”,html)
打印(新的html)
new_html=html.replace(“”“”)
打印(新的html)

您能否在字符串替换之前和之后添加一个引号,以便我们更好地了解您想要实现的目标?我想将字符串更改为blankIt should is“,它包含一个额外的引号
不会改变任何东西。它仍然有效。我编辑了这个问题。我觉得这很奇怪。我复制了你的代码来运行它。它是有效的。但是,我在代码中使用它。它不起作用……别忘了将额外的
添加到替换字符串中。
new_html_code_02 = re.sub('<div class=\"\"ContentGrid\"\">', " ", new_html_code_01)
new_html_code_02 = re.sub('<div class=""ContentGrid"">', " ", new_html_code_01)
import re


html = '<div class=""ContentGrid"">' \
       '<img data-icons="":~("" src=""' \
       '"" onload=""DrawImage(this)"" width=""300"" height=""617"">'

new_html = re.sub('<div class=""ContentGrid"">', " ", html)
print(new_html)

new_html = html.replace('<div class=""ContentGrid"">', " ")
print(new_html)