944,117 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 525
  • Python RSS
Nov 3rd, 2009
0

File to dictionary

Expand Post »
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:
Python Syntax (Toggle Plain Text)
  1. def file_to_dict(f):
  2. for line in f:
  3. columns = line.split(",")
  4. print columns[0]

Your help is appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
pyprog is offline Offline
31 posts
since Oct 2009
Nov 3rd, 2009
0
Re: File to dictionary
Hint:

Python Syntax (Toggle Plain Text)
  1. >>> l = []
  2. >>> l += [ "a" ]
  3. >>> l
  4. ['a']
  5. >>>

Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Nov 3rd, 2009
0
Re: File to dictionary
Hint:

Python Syntax (Toggle Plain Text)
  1. >>> l = []
  2. >>> l += [ "a" ]
  3. >>> l
  4. ['a']
  5. >>>

I did the following
Python Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
pyprog is offline Offline
31 posts
since Oct 2009
Nov 3rd, 2009
0
Re: File to dictionary
hi again,
the problem is that you have to add the letter in the list while you are in the for loop

Python Syntax (Toggle Plain Text)
  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 ]
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Nov 3rd, 2009
0
Re: File to dictionary
hi again,
the problem is that you have to add the letter in the list while you are in the for loop

Python Syntax (Toggle Plain Text)
  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...
Reputation Points: 10
Solved Threads: 0
Light Poster
pyprog is offline Offline
31 posts
since Oct 2009
Nov 3rd, 2009
0
Re: File to dictionary
The more common approach would be to use append() ...
python Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 3rd, 2009
0
Re: File to dictionary
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.

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is online now Online
773 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: With with Pascals Triangle Program
Next Thread in Python Forum Timeline: update namespace





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC