1

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!

3
  • 3
    This is homework right? Commented Jul 6, 2017 at 18:48
  • 2
    I 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 question Commented 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 help Commented Jul 6, 2017 at 18:50

4 Answers 4

6

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)]
Sign up to request clarification or add additional context in comments.

Comments

3

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

This is the most correct answer, no modules needed, no loading the entire file into memory, and closes the file properly!
@OferSadan I was literally looking at my answer debating if I wanted to add the list comprehension edit. Kudos for bolstering my thought!
@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.
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.
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.
|
0

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

This loads the entire file into memory (because of readlines) and also doesn't close it properly.
0

This may not be the most efficient solution, but you could also just hard code it e.g. create a variable equivalent to zero, add one to the variable for each word in the line, and append the word to a list when variable = 5. Then reset the variable equal to zero.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.