Jump to content

The last poster wins


TheCalliton

Recommended Posts

Hey, anyone know how to make this work in Python?

 

I've got to import a .txt file, substitute words in it, then export as .html.

 

This one below works...

 

from re import sub

out = open("mytest.html","w")
data = open("mytest.txt").read()
out.write(sub("hello","goodbye",data))
out.close()

 

But, this one doesn't.

 

from re import sub

out = open("mytest.html","w")
data = open("mytest.txt").read()
out.write(sub("--","–",data))
out.close()

 

The problem is that the symbols in the substitution are messing it up. How exactly do I make "–" read as a raw bit of text?

 

 

EDIT: Found it out. There needed to be a prefix r for it to work, like r"–". So now that I've got that working, I've stumbled upon another thing.

 

When I use the above function now, it duplicates all the stuff instead of substituting it. I have no idea how I'm going to go about fixing that. :/

 

 

EDIT 2: Ok, so I reckon the problem is in out.write(sub(...)). I tried using other ones like replace but that didn't work. Damn this stupid evil python.

 

 

EDIT 3: Aha, so I've got that to work. Now I'm up to me neck in quicksand again.

Does anyone here know Python Regular Expressions well? If not, oh well, I'll just suffer in misery.

Edited by billyro
Link to comment
Share on other sites

Hey check out the main forum at Precursor games, look at the right side and the timer :smile:

 

http://www.precursorgames.com/forums/index.php

 

and this

 

http://www.youtube.com/watch?feature=player_embedded&v=BVvWWBM7woA#!">http://www.youtube.com/watch?feature=player_embedded&v=BVvWWBM7woA#!" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="295">

Edited by Thor.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...