2 ms·
opening and reading a file: in_file = open("/path/to/file.txt", mode="r") out_file = open("/path/to/file.html", mode="w") out_file.write("
by jiaaro 16y ago
opening and reading a file:
in_file = open("/path/to/file.txt", mode="r")
out_file = open("/path/to/file.html", mode="w")
out_file.write("<html><head><title>my doc</title></head><body>")
for line in in_file.readlines():
line = "<p>" + line + "</p>
out_file.write(line)
out_file.write("</body></html>")
in_file.close()
out_file.close()
a few tips:
getting the attributes of anything at all: dir()
using the file object above:
>>> dir(in_file)
...
['__class__',
'__delattr__',
'__doc__',
'__enter__',
'__exit__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__iter__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'close',
'closed',
'encoding',
'errors',
'fileno',
'flush',
'isatty',
'mode',
'name',
'newlines',
'next',
'read',
'readinto',
'readline',
'readlines',
'seek',
'softspace',
'tell',
'truncate',
'write',
'writelines',
'xreadlines']
getting the doc string: help()
>>> help(in_file.write)
... Help on built-in function write:
write(...)
write(str) -> None. Write string str to file.
Note that due to buffering, flush() or close() may be needed before
the file on disk reflects the data written.
- Random_Person 16y agoThanks! I figured out the open problem.. which was some sort of unicode issue where I had the file saved in c:\Users\.. and I guess when it hit the \U it generated an escape. that's a pretty weird function. So, moved the file and renamed and fixed that issue. 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.
- zephyrfalcon 16y agoThis is more a question for StackOverflow, but here goes... 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.
- Random_Person 16y agoToo awesome. As a linux user, I feel ashamed that I never tried to use forward slashes in Windows. 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!