I have a v. large text document (no file extension) that has information about a different file on every line in the following format:
VariableOne|VariableTwo|VaraibleThree
The pipe separates the different variables.
However, within some ‘VaraibleTwo’s there may also be pipes.
I need to extract that information from the text document so that I can manipulate the information. For example:
Name = VariableOne From The Text Document
Middle Name(s) = VariableTwo From The Text Document
Last Name = VariableThree From The Text Document
This needs to be done in Python 3 with six variables all together and only the second variable containing pipes.
Thanks for any help you can give!
How about:
where you could replace the explicit naming by slicing the split result, which might be a little more general. As for reading the file, the usual pattern is
which doesn’t read the entire file into memory at once, which is handy for large files.
UPDATE: if you have to loop over all the lines, but only process one, then you could use
On the other hand, if there’s only one line you need to extract and process, you can use the linecache module:
Although to be honest, I think I prefer Karl Knechtel’s two-argument split approach because it’s more general.