by Random_Person · 16y ago · 0 comments
So, on my own, I had gotten as far as starting the for loop and I was unaware that 'line' actually gave me a workable string. What I was working on was this:
for line in in_file:
tx = in_file.readlines()
because my output needs to be pieces of the line instead of the whole line at a time-- so something like this: for line in in_file:
tx = in_file.readlines()
out_file.write("<tr bgcolor="006699">"
out_file.write("<td>" + tx[1,10] + "</td>")
out_file.write("<td>" + tx[12, 23] + "</td>")
and so on.Since Windows paths use backslashes, you should either escape all of them (not just the \U), or use raw strings:
filename = "c:\\Users\\foo.txt"
filename = r"c:\Users\foo.txt"
As it happens, Unix-style forward slashes usually work fine too on Windows: filename = "c:/Users/foo.txt"
This way, you don't have to rename your files and directories. :-)Also, if the contents of the line are delimited by spaces, consider the split() method:
"a b c".split()
=> ["a", "b", "c"]
If the format isn't actually as easy as that, take a look at the re and csv modules.I really like that split thing. I'll have to do some more research into it. Figuring out how to do that and assign each split to its own variable and all...
Thanks a ton!