File to dictionary

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 18
Reputation: pyprog is an unknown quantity at this point 
Solved Threads: 0
pyprog pyprog is offline Offline
Newbie Poster

File to dictionary

 
0
  #1
Nov 3rd, 2009
If I have a file that has a content of this kind:
a,b
c,d
e,f
g,h

I need to make a list of the letters in the first column, i.e., [a, c, e, g]. I wrote some code which prints these letters but I don't know how to put them into a list. Here is the code:
  1. def file_to_dict(f):
  2. for line in f:
  3. columns = line.split(",")
  4. print columns[0]

Your help is appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 241
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 58
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #2
Nov 3rd, 2009
Hint:

  1. >>> l = []
  2. >>> l += [ "a" ]
  3. >>> l
  4. ['a']
  5. >>>

My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: pyprog is an unknown quantity at this point 
Solved Threads: 0
pyprog pyprog is offline Offline
Newbie Poster
 
0
  #3
Nov 3rd, 2009
Originally Posted by masterofpuppets View Post
Hint:

  1. >>> l = []
  2. >>> l += [ "a" ]
  3. >>> l
  4. ['a']
  5. >>>

I did the following
  1. def file_to_dict(f):
  2. list1 = []
  3. for line in f:
  4. columns = line.split(",")
  5. letters = columns[1]
  6. list1 += [letters]
  7. print list1
but the output is just ['g']. It always prints only the last letter. How to print them all in a list?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 241
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 58
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #4
Nov 3rd, 2009
hi again,
the problem is that you have to add the letter in the list while you are in the for loop

  1. for line in f:
  2. columns = line.split(",")
  3. letters = columns[ 0 ]
  4. list1 += [ letters ]

also I am not sure why you put columns[ 1 ], I thought you wanted the first element, which is columns[ 0 ]
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: pyprog is an unknown quantity at this point 
Solved Threads: 0
pyprog pyprog is offline Offline
Newbie Poster
 
0
  #5
Nov 3rd, 2009
Originally Posted by masterofpuppets View Post
hi again,
the problem is that you have to add the letter in the list while you are in the for loop

  1. for line in f:
  2. columns = line.split(",")
  3. letters = columns[ 0 ]
  4. list1 += [ letters ]

also I am not sure why you put columns[ 1 ], I thought you wanted the first element, which is columns[ 0 ]
yeah sorry...never mind the [1]...my mistake...thank you for your help...much appreciated...
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,070
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite
 
0
  #6
Nov 3rd, 2009
The more common approach would be to use append() ...
  1. for line in f:
  2. columns = line.split(",")
  3. letters = columns[ 0 ]
  4. list1.append(letters)
I think it is less mistake prone. I also want to point out, that should you want to read the second column (the end of each line), you will find a newline character attached.

Since you title is File to dictionary this may become important.
Last edited by vegaseat; Nov 3rd, 2009 at 5:00 pm. Reason: newline
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 167
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 49
snippsat's Avatar
snippsat snippsat is offline Offline
Junior Poster
 
0
  #7
Nov 3rd, 2009
To make it short with python list comprehensions.
Not that this make code better or more readable in many cases.
But think this line is not so bad to understand.

  1. >>> f = [line[0] for line in open('ab.txt')]
  2. >>> f
  3. ['a', 'c', 'e', 'g']
  4. >>>

Read every characters.
So colum 2 will be line[2]
Last edited by snippsat; Nov 3rd, 2009 at 7:04 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC