I am very new to python with no real prior programing knowledge. At my current job I am being asked to take data in the form of text from about 500+ files and plot them out. I understand the plotting to a degree, but I cannot seem to figure out how to manipulate the data in a way that it is easy to select specific sections. Currently this is what I have for opening a file:
fp=open("file")
for line in fp:
words = line.strip().split()
print words
The result is it gives me a list for each line of the file, but I can only access the last line made. Does any one know a way that would allow me to choose different variations of lists? Thanks a lot!!
The easiest way to get a list of lines from a file is as follows:
Now you can split those lines or do whatever you want with them:
I’m not certain that answers your question — let me know if you have something more specific in mind.
Since I don’t understand exactly what you are asking, here are a few more examples of how you might process a text file. You can experiment with these in the interactive interpreter, which you can generally access just by typing ‘python’ at the command line.
That’s the raw, unprocessed text of the file. It’s a string. Strings are immutable — they can’t be altered — but they can be copied in part or in whole.
splitlinesis a string method.splitlinessplits the string wherever it finds a\n(newline) character; it then returns a list containing copies of the separate sections of the string.Here I’ve just saved the above list of lines to a new variable name.
Lists are accessed by indexing. Just provide an integer from
0tolen(lines) - 1and the corresponding line is returned.Now you can start to manipulate individual lines.
splitis another string method. It’s likesplitlinesbut you can specify the character or string that you want to use as the demarcator.You can also index the characters in a string.
You can also “slice” a string. The result is a copy of characters 4 through 9. 10 is the stop value, so the 10th character isn’t included in the slice. (You can slice lists too.)
If you want to find a substring within a string, one way is to use
index. It returns the index at which the first occurrence of the substring appears. (It throws an error if the substring isn’t in the string. If you don’t want that, usefind, which returns a -1 if the substring isn’t in the string.)Then you can use that to slice the string. If you don’t provide a stop index, it just returns the remainder of the string.
If you don’t provide a start index, it returns the beginning of the string and stops at the stop index.
You can also use
in— it’s a boolean operation that returnsTrueif a substring is in a string. In this case, I’ve used a list comprehension to get only the lines that have'line'in them. (Note that the last line is missing from the list. It has been filtered.)Let me know if you have any more questions.