如何在Python中用整个文件中的新字符串更改字符串

时间:2015-06-26 15:45:46

标签: python regex

我之前问过这个问题:Getting a number from the string and using it to replace the text in Python。但我仍然面临一个问题。我需要在整个文件中使用新字符串更改字符串,但我无法执行此操作。当我打印出数据或将其写入新文件时,我无法看到更改生效。不确定是什么问题。

这是我先试过的:

with open('/Users/lakesh/Downloads/qz_question.txt', 'r') as fin:
    filedata = fin.read()

filedata1 = re.sub(r'<img\b[^<]*(?<=title=")\\frac\{(\d+)\}\{(\d+)\}"[^<]*>', "<sup>\g<1></sup>&frasl;<sub>\g<2></sub>", filedata)
print filedata1

# Write the file out again
with open('/Users/lakesh/Downloads/qz_question1.txt', 'w') as fin:
  fin.write(filedata1)

我已将替换的单词分配给另一个变量,但新变量仍然具有相同的字符串,它似乎不会被替换。我试图用文件名替换。我也试图逐行替换:

for line in filedata:
    re.sub(r'<img\b[^<]*(?<=title=")\\frac\{(\d+)\}\{(\d+)\}"[^<]*>', "<sup>\g<1></sup>&frasl;<sub>\g<2></sub>", line)

print filedata

qz_question1.txt仍包含img标记。它似乎没有被替换。需要一些指导。

qz_question有大量文字,其中一行看起来像这样:

(163, 'S001', 'T005', 'ST015', 'Medium', '1', 9, '1', '<p>The ratio of the number of children to the number of adults at a funfair was 2 : 5.​&nbsp;&nbsp;<sup>1</sup>&frasl;<sub>5</sub>of the children were boys. If there were 120 more adults than children, how many girls were there at the funfair?</p>\n<p>&nbsp;</p>', 'without_image', '[{"value":"16","answer":"0"},{"value":"40","answer":"0"},{"value":"64","answer":"1"},{"value":"120","answer":"0"}]', '<p>5 -2 = 3</p>\n<p>3 units --&gt; 120</p>\n<p>1 unit --&gt; 120 &divide; 3 = 40</p>\n<p>2 units --&gt; 40 x 2 = 80</p>\n<p>1 - <img title="\\small \\frac{1}{5}" src="http://latex.codecogs.com/gif.latex?\\small&amp;space;\\frac{1}{5}" alt="" width="5" height="20" />&nbsp;=&nbsp;<img title="\\small \\frac{4}{5}" src="http://latex.codecogs.com/gif.latex?\\small&amp;space;\\frac{4}{5}" alt="" width="4" height="16" /></p>\n<p><img title="\\small \\frac{4}{5}" src="http://latex.codecogs.com/gif.latex?\\small&amp;space;\\frac{4}{5}" alt="" width="4" height="16" />&nbsp;x 80 = 64</p>', 'lakshmen K', NULL, '1', '0', '2015-05-03 15:54:19', '0000-00-00 00:00:00'),

2 个答案:

答案 0 :(得分:0)

如何使用替换?

如果您可以粘贴文件的副本以及您希望将字符串转置为的内容,我将更正replace

with open('/Users/lakesh/Downloads/qz_question.txt', 'r') as fin:
    filedata = fin.readlines()

for line in filedata:
    line.replace('<img', '<sup')
    line.replace('</img>', '</sup>')

然后覆盖或保存到新文件。

答案 1 :(得分:0)

从我看到的问题出现在正则表达式中因为在\ frac之前存在\ small。 相反,代码语法python是正确的。 你可以查看:

{{1}}

返回