Coding, Python

Python fun #2 : Input directly from a text file to a List (in 1 statement)

Assuming the text file(names.txt) has the data as strings separated by comma

L = list(eval(open(‘names.txt’).readlines()[0]))

what it is doing?

It is inputting the whole file as a single string. It outputs a Tuple.

The list() method converting that into a nice List.

You can use it by changing slightly according to your input file format.

Advertisements

2 thoughts on “Python fun #2 : Input directly from a text file to a List (in 1 statement)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s