| | |
File to dictionary
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
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:
Your help is appreciated.
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)
def file_to_dict(f): for line in f: columns = line.split(",") print columns[0]
Your help is appreciated.
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#3 Nov 3rd, 2009
I did the following
but the output is just ['g']. It always prints only the last letter. How to print them all in a list?
Python Syntax (Toggle Plain Text)
def file_to_dict(f): list1 = [] for line in f: columns = line.split(",") letters = columns[1] list1 += [letters] print list1
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
also I am not sure why you put columns[ 1 ], I thought you wanted the first element, which is columns[ 0 ]
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)
for line in f: columns = line.split(",") letters = columns[ 0 ] list1 += [ letters ]
also I am not sure why you put columns[ 1 ], I thought you wanted the first element, which is columns[ 0 ]
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#5 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
Python Syntax (Toggle Plain Text)
for line in f: columns = line.split(",") letters = columns[ 0 ] list1 += [ letters ]
also I am not sure why you put columns[ 1 ], I thought you wanted the first element, which is columns[ 0 ]
0
#6 Nov 3rd, 2009
The more common approach would be to use append() ...
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.
python Syntax (Toggle Plain Text)
for line in f: columns = line.split(",") letters = columns[ 0 ] list1.append(letters)
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!
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.
Read every characters.
So colum 2 will be line[2]
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)
>>> f = [line[0] for line in open('ab.txt')] >>> f ['a', 'c', 'e', 'g'] >>>
Read every characters.
So colum 2 will be line[2]
Last edited by snippsat; Nov 3rd, 2009 at 7:04 pm.
![]() |
Similar Threads
- Read text file as dictionary (Python)
- Iterating through a fasta file dictionary made using Biopython (Python)
- How to extract value from a text file (Python)
- text file to dictionary (Python)
- File opening error (Python)
- how do you iterate though a file on a shelf? (Python)
Other Threads in the Python Forum
- Previous Thread: With with Pascals Triangle Program
- Next Thread: update namespace
Views: 305 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied advanced argv beginner change code command convert csv cursor def dictionary digital dynamic dynamically editing enter event examples excel file float format frange ftp function google gui homework i/o import input jaunty java keyboard lapse line linux list lists loop microphone mouse newb number numbers obexftp output panel parameters parsing path port prime program programming projects py2exe pygame pygtk pyopengl pyqt python random recursion recursive remote return scrolledtext session skinning sprite ssh stderr string strings strip subprocess syntax table tennis terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape windows wxpython






