Greytree

TamWiki

For a mouse who is a packrat

Technology » Read A File Into A List Stripping Newlines In Python
code snippet for reading a file into an list stripping newlines in Python

Summary:this is what goes at the top of the site

(redirected from Main.ReadAFileIntoAListStrippingNewlines)

<< Prev: Ipython

Up: ^Python^

Using list comprehension:

  1. with open("filespec") as f:
  2.     lines = [x.strip for x in f.readlines()]

Contrast with the perl idiom:

  1. open (FH, "<filespec");
  2. @lines = <FH>;
  3. chomp(@lines);

And the same in Ruby:

lines = IO.readlines("filespec").map{|l| l.chomp}


Tags: Categories: ComputerStuff

Recent Changes | Printable View | Page History | Edit Page
Page last modified on April 16, 2012, at 11:43 AM by tamara