Read a file, skip unwanted lines & add into a List

I have the following code where I scan every line and put in a list. If the line matches a string "New changes", I don't want to put in the list. Any suggestion on how to achieve this?

with open('file.txt', 'rb') as f:
    mainlist = [line.strip() for line in f]

Answers


List comprehensions can also do filtering:

mainlist = [line.strip() for line in f if "New changes" not in line]

You can filter within the list comprehension :

mainlist = [line.strip() for line in f if line.strip() !=  "New changes"]

Need Your Help

How to show an alert when the server is not working properly?

iphone objective-c uialertview

I am getting list using url by doing parsing it using xml parser. sometimes the server is not working properly. then how to give an alert when server is not working properly. I have given the code ...

Vector of Vector Initialization

c++ vector initialization deserialization

I am having a tough time getting my head wrapped around how to initialize a vector of vectors.

ArgumentException when adding many coordinates on the map

c# map windows-phone-8 location

I would like to add some pushpins that can be clickable on the map. First, I would like to display them but when I add them on a map, an ArgumentException is occured and my application crashes.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.