| | |
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 34 Days Ago
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 34 Days Ago
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 34 Days Ago
•
•
•
•
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 34 Days Ago
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; 34 Days Ago at 5:00 pm. Reason: newline
May 'the Google' be with you!
•
•
Join Date: Aug 2008
Posts: 163
Reputation:
Solved Threads: 49
0
#7 34 Days Ago
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; 34 Days Ago 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
| Thread Tools | Search this Thread |
accessdenied advanced application argv beginner change color command convert csv cursor def dictionary digital dynamic dynamically edit editing enter event examples excel file float format frange function google gui homework i/o import input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame newb number numbers numeric obexftp output parameters parsing path port prime programming projects py2exe pygame pygtk pyopengl python random recursion remote return reverse scrolledtext session simple skinning smtp sprite stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape windows wxpython






