开发者

BeautifulSoup chokes on paths with back slashes

I wrote a script to automate the process of creating an image gallery. I used os.path.join() for creating paths to new image directories.

I only relized after creating 开发者_开发问答all the galleries that using os.path.join() was not such a good idea as it creates paths with \ (on windows) which causes problems with firefox (it doesn't seem to understand the path format and cant find the images).

Id rather not have to create all the galleries again since the gallery headers have to be entered manually. I thought BeautifulSoups prettify() would fix the paths but it chokes on the backslashes. e.g.

input:

<td><a rel="group" href="images\042.jpg"><img class="gimage" src="images\thumbnails\thumb_042.jpg" alt=""></a></td>

output:

<td>
 <a rel="example_group" href="images">
  <img class="gimage" src="images   humbnails   humb_042.jpg" alt="" />
 </a>
</td>

How can I fix the paths?


In this case, per the comments, it appears that the problem can be solved with a global substitution of / for \:

import fileinput
import sys
for line in fileinput.input(['test.html'], inplace=True, backup='.bak'):
    sys.stdout.write(line.replace('\\','/'))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜