gsub允许在gsub中使用双引号

时间:2016-12-29 20:11:58

标签: ruby ruby-on-rails-4 ruby-on-rails-5 gsub

我有这个

self.description.gsub! 'iframe', 'iframe id="myid" '

当我保存时它会输出:

iframe id=\"myid\"

但在每个"

之前我都不想要反斜杠\

我该怎么做?

我如何允许2 x"内?

1 个答案:

答案 0 :(得分:2)

我不知道你是否在IRB中运行它,但有时你必须打印以查看字符串的样子:

df = pd.read_csv('filename.txt', sep=";", names=['Region Name'])
df.insert(0, 'State', df['Region Name'].str.extract('(.*)\[edit\]', expand=False).ffill())
df = df[~df['Region Name'].str.contains('\[edit\]')].reset_index(drop=True)
print (df)
      State                                        Region Name
0   Alabama                      Auburn (Auburn University)[1]
1   Alabama             Florence (University of North Alabama)
2   Alabama    Jacksonville (Jacksonville State University)[2]
3   Alabama         Livingston (University of West Alabama)[2]
4   Alabama           Montevallo (University of Montevallo)[2]
5   Alabama                          Troy (Troy University)[2]
6   Alabama  Tuscaloosa (University of Alabama, Stillman Co...
7   Alabama                  Tuskegee (Tuskegee University)[5]
8    Alaska      Fairbanks (University of Alaska Fairbanks)[2]
9   Arizona         Flagstaff (Northern Arizona University)[6]
10  Arizona                   Tempe (Arizona State University)
11  Arizona                     Tucson (University of Arizona)

请注意,gsub的返回值是转义双引号,但这是因为整个字符串用双引号括起来,但打印的值虽然不同。