0

I have a text file called urldata.txt which I am opening and reading line by line. I wrote a for loop to read it line by line, but I want to save the output I receive as a list. Here is what I have:

textdata = open("urldata.txt","r")
for line in textdata:
    print(line)

this returns:

http://www.google.com

https://twitter.com/search?q=%23ASUcis355

https://github.com/asu-cis-355/course-info

I want to save these lines above as a list. Any suggestions? I have tried appending and such, however, being new to Python I'm not sure how to go about this.

2
  • 1
    What do you mean by you want to save it as a list? What exact output are you expecting in your file? Please give a sample output. Also, please provide that part of the code that you wrote to attempt to solve this. Commented Mar 19, 2016 at 4:11
  • I want to save it in a way that I can manipulate each line. In the end I need to break each line apart into 3 different parts. So, it would help me if it was saved somehow to manipulate it later on. Does that help? Commented Mar 19, 2016 at 4:13

2 Answers 2

1

You just want a list of every line of the file?

urls = open("urldata.txt").read().splitlines() 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes! Exactly, this helped me a ton. I guess I didn't need a for loop after all!
@JessicaTylka please use the checkmark next to the answer to show your thanks
0

If you just want the lines as a list, that's trivial:

with open("urldata.txt") as textdata:
    lines = list(textdata)

If you want newlines stripped, use a list comprehension to do it:

with open("urldata.txt") as textdata:
    lines = [line.rstrip('\r\n') for line in textdata]

8 Comments

What about splitlines?
@cricket_007: I prefer not to double peak memory usage, which is what a slurp followed by splitlines involves. Reading line by line and stripping is slightly slower in some cases, but handles huge files better when RAM is tight. Ideally, you wouldn't actually listify, just iterate directly.
Fair point. The question did only have 3 data points, though. I would only be worried about the memory at the point of thousands of lines
@TigerhawkT3: readlines() is redundant with the list() constructor, and I prefer to avoid it because it often gives people the mistaken impression that that is how you are "supposed" to get the lines from file objects. In fact, in most cases, you're better off iterating and processing as you go for scalability; using the list constructor makes it more clear that file objects are "just another type of iterator" that list can consume to initialize itself; you could skip list() and just iterate the lines directly. Don't want to encourage terrible code like for line in f.readlines():
@cricket_007: True. I just prefer to assume the question is a toy version of a problem, and the code may need to work with much larger data. I want the code to work in as many cases as possible first and be relatively simple; other concerns are tertiary.
|

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.