| | |
File to dictionary
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 26 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 26 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 26 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 26 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; 26 Days Ago at 5:00 pm. Reason: newline
May 'the Google' be with you!
•
•
Join Date: Aug 2008
Posts: 151
Reputation:
Solved Threads: 46
0
#7 26 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; 26 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 |
alarm app beginner cipher cmd coordinates cx-freeze data decimals development dictionary directory dynamic error examples feet file float format function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation split sqlite ssh string strings sudokusolver terminal text thread threading time tlapse tooltip tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia wx.wizard wxpython xlwt






