Anyone wanna help me learn Python?
Today, an opportunity presented itself for me to write what I assumed would be a quick little script, yet it has turned into a few hours of frustration. This is something I used to be able to do in Basic/VB in a few minutes, but I can't even get Python to open a file.
What I've got: large plain txt document space delimited
What I need: Import the text, line for line, and output to another file with HTML tags.
Simple, right? I understand the theory of how to do this, but can't get it to work.
Open the original file as read. Open the output file as append. I can't even get past that basic concept.
Then, for loop for each line of original? make string variable for line w.write to output file with my HTML code and text from original file?
I believe I'm on the right track, but have no idea how to write this dang thing. Documentation isn't helping as I can't even seem to get the dang open command to work.
Any help?
14 comments
[ 7.0 ms ] story [ 54.3 ms ] threadgetting the attributes of anything at all: dir()
using the file object above:
getting the doc string: help()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:
because my output needs to be pieces of the line instead of the whole line at a time-- so something like this: and so on.Since Windows paths use backslashes, you should either escape all of them (not just the \U), or use raw strings:
As it happens, Unix-style forward slashes usually work fine too on Windows: 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:
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!
What I can't grasp is why write doesn't start a newline and what I can do to remedy that. Currently this is just making a huge line of text instead of individual lines.
Thanks a ton!
Okay, so, stripping spaces from string values. Easy? If I import my line, I have 6 strings I need to segment out of it and my file uses spaces. So, in my previous line:
I'd like to strip the spaces from line[0:12]Thanks!
Here is the final script for anyone who cares. Can it be optimized?
- Prefer Python's string formatting operator. I even prefer the extended dict-compatible syntax.
- close() is called automatically when the filehandles are garbage-collected; you can usually omit it for tasks like this.
- ranges (like those in brackets) are exclusive, not inclusive. So I suspect you have an off-by-one error there.
Thus:
I love the template thing. I've got to read up on that!
On close() - I found that if I didn't execute a close, and I opened the output in a text editor and attempted to save it I got an error that it was being accessed by another process.
I love this community, you guys are awesome. Thank you.
I too had similar problems getting a hang of Python. "Dive into Python" was a help. But once it 'clicked' it became hard to write anything that didn't work. One thing that I still marvel is at, that you can often just make a guess and it works! The collection of 'batteries' is very useful. Typing help(some_function_or_class) at the command prompt is often very useful.
NB: if you are writing for Windows and Linux and OS/X, then you should use the functions in os.path module to avoid hard coding the directory and filename conventions.
This was a one-off for that particular task, so no cross platform writing, but I'm certainly use os.path in the future. Thanks for that!