r/pythonhelp • u/cheyanneshariat • Oct 26 '20
Python Parsing help
Hey all,
I am very new to python and basically have a project with text files. I want to store the first word in a line, and then search for that word in another file. Then, once I find the line that contains that word, I want to print specific words from the line (which is separated by whitespaces) into another file.
For example, the first word in my first line of my first file is "bird". I then search for the line that contains "bird" in my second file (assuming only one does) and I get the words that are between the 4th and 5th space in that line and print it in another file.
I do know the basic of stripping line and reading/writing files, but I do not know how to basically make the for loop part of get the word between specific spaces. Anything helps, sorry I am a noob.
Thank you in advance!
0
u/itamarc137 Oct 29 '20
I recommend this discord server: https://discord.gg/3Mp8Vt You will eaisly get help there!
1
u/run_and_time Oct 27 '20
I’m still a teeny bit confused about the project but it sounds like you’ll want to use split(). If you call it on, for example, ‘this is fun’.split(‘ ‘), it will split the string into a list of [‘this’, ‘is’, ‘fun’], without the whitespaces. When calling split you can specify the delimiter you’re splitting the string up on, in this case it’s a space (‘ ‘). Hopefully this can help you work towards the right direction.