Dictionary help

Tim Chase python.list at tim.thechases.com
Tue Feb 18 13:46:26 EST 2014


On 2014年02月18日 10:30, kjakupak at gmail.com wrote:
> So let's say I have a file and it looks like this:
> Title 1: item 
> Title 2: item 
> etc
>> Is it possible to use a dictionary for something like the input
> above? Because I want to be able to use the input above to delete
> the "Title 1" and "Title 2" but still show the items (on separate
> lines). Basically I want it to just show: item item 
> etc..
>> What I've got is 
> dict(item.split(":") for item in cInfo.split(" "))
>> Where cInfo is a function that extracts the first 5 lines of a file

It sounds like all you need is some basic string functions, not a
dictionary:
 for line in file('input.txt'):
 title, _, item = line.partition(':')
 print(item.strip())
-tkc


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /