What would be a pythonic way to create a list of (to illustrate with an example) the fifth string of every line of a text file, assuming it ressembles something like this:
12, 27.i, 3, 6.7, Hello, 438
In this case, the script would add "Hello" (without quotes) to the list.
In other words (to generalize), with an input "input.txt", how could I get a list in python that takes the nth string (n being a defined number) of every line?
Many thanks in advance!
-
3This is homework right?Ofer Sadan– Ofer Sadan2017-07-06 18:48:16 +00:00Commented Jul 6, 2017 at 18:48
-
2I can understand why you'd think that haha, but I'll only start taking CS courses next year. I'm trying to learn the ropes right now, which is why I came with what I assume is a fairly basic questionRealFL– RealFL2017-07-06 18:49:43 +00:00Commented Jul 6, 2017 at 18:49
-
That's ok, welcome to SO, next time you should ask specific questions about coding, with your own work and problems you encountered, and we'll do out best to helpOfer Sadan– Ofer Sadan2017-07-06 18:50:58 +00:00Commented Jul 6, 2017 at 18:50
Add a comment
|
4 Answers
You could use the csv module to read the file, and store all items in the fifth column in a list:
import csv
with open(my_file) as f:
lst = [row[4] for row in csv.reader(f)]
Comments
If its a text file it can be as simple as:
with open(my_file, 'r') as f:
mylist = [line.split(',')[4] for line in f] # adds the 5th element of split to my_list
6 Comments
Ofer Sadan
This is the most correct answer, no modules needed, no loading the entire file into memory, and closes the file properly!
pstatix
@OferSadan I was literally looking at my answer debating if I wanted to add the list comprehension edit. Kudos for bolstering my thought!
juanpa.arrivillaga
@OferSadan I wouldn't say "no modules needed" is "correct". Indeed, this is the cardinal sin of "reinventing the wheel". Not using a well-tested and very efficient standard-library module and instead reimplementing its functionality from scratch is not a virtue. It's the sort of thing that gets you eviscerated in a code-review.
Ofer Sadan
On that part I agree it's just an opinion - but in my mind for simple problems there's no requirement to use external modules. This of course can be very wrong for large problems, but the problem in question was quite simple. Regardless, this answer has all the other virtues I mentioned.
juanpa.arrivillaga
What do you mean by "external modules"? Why wouldn't you use
csv? It's part of the standard library! It's as much a part of the language as open itself! I agree with the other virtues whole-heatedly, though. |
Since you mentioned that you are using a .txt file, you can try this:
f = open('filename.txt').readlines()
f = [i.strip('\n').split(",") for i in f]
new_f = [i[4] for i in f]
1 Comment
Ofer Sadan
This loads the entire file into memory (because of
readlines) and also doesn't close it properly.