| | |
text file to dictionary
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 10
Reputation:
Solved Threads: 0
test.txt
ok i want to read this file into a dictionary and this is the output
d={"flight":T34712, From:ABERDEEN, scheduled 0800, remark landed}
etc
flight is the key
do i have to parse the text file i know i cant put it straight into the dictionary as i get this output {flightfromscheduledremark: whole file}
if i have to parse it can anyone give me a couple of links please.
or can i read the text file word by word and use a while loop like
while (not end of file)
if word is first in sentance
d.append{flight:test}
heres my code currently if anyone wants to look and criticise.
ok i want to read this file into a dictionary and this is the output
d={"flight":T34712, From:ABERDEEN, scheduled 0800, remark landed}
etc
flight is the key
do i have to parse the text file i know i cant put it straight into the dictionary as i get this output {flightfromscheduledremark: whole file}
if i have to parse it can anyone give me a couple of links please.
or can i read the text file word by word and use a while loop like
while (not end of file)
if word is first in sentance
d.append{flight:test}
heres my code currently if anyone wants to look and criticise.
python Syntax (Toggle Plain Text)
#!/usr/bin/env python import HTMLParser class MyParser(HTMLParser.HTMLParser): ######################################################## def __init__(self): HTMLParser.HTMLParser.__init__(self) self.titleFound = False return ######################################################## def handle_starttag(self, tag, attrs) if tag == 'td': self.titleFound = True return ########################################################## def handle_data(self, titleString): if self.titleFound == True: filename = "test.csv" f = open(filename, 'a') f.write(titleString) f.close() return ########################################################## def handle_endtag(self, tag): if tag == 'td': self.titleFound = False return ###################End of Class definition ####################### if __name__ == '__main__': titleExtractor = MyParser() buffer = open('live.html', 'r').read() titleExtractor.feed(buffer) filename = "test.csv" f = open(filename,'r') test = f.read() d={"flight":test} print d
Last edited by lilkid; Mar 27th, 2009 at 2:48 pm.
You can read the text file word by word! 
Hows that? Im a bit confused by exactly what you want but that will read a text file by each word. Replace the if statement with your own one that does something more relevant.

python Syntax (Toggle Plain Text)
d = {'flight':[]} f = #text file for line in f: for word in line.split(): if word == what_you_want: d['flight'].append(word)
Make it idiot-proof, and someone will just make a better idiot
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Mar 2009
Posts: 10
Reputation:
Solved Threads: 0
T34712 ABERDEEN 0800 LANDED 08:00
BE171 SOUTHAMPTON 0820 LANDED 08:07
here are 2 lines in the txt file i want to put it into a dictionary so the output is
d={"flight":T34712, From:ABERDEEN, scheduled 0800, remark landed}
d={"flight":BE171 , From: SOUTHAMPTON, scheduled 0820, remark landed}
with flights as the dictionary key
thanks for your suggestion ill try it out and reply tomorrow
BE171 SOUTHAMPTON 0820 LANDED 08:07
here are 2 lines in the txt file i want to put it into a dictionary so the output is
d={"flight":T34712, From:ABERDEEN, scheduled 0800, remark landed}
d={"flight":BE171 , From: SOUTHAMPTON, scheduled 0820, remark landed}
with flights as the dictionary key
thanks for your suggestion ill try it out and reply tomorrow
Last edited by lilkid; Mar 27th, 2009 at 5:43 pm.
EDIT: The below isn't too helpful as vegaseat posted a better (and full) verison.
What do you mean by
The other keys need to be enclosed in quotes to, so that
The best solution I think is paulthom12345's suggestion. Although, I'm still confused about the set-up you seem to want. There's a better way to organize it, such as one list with each index a separate flight, and each of those indices holding a dict with the keys for 'flight', 'from', 'scheduled', 'remark', etc. Like:
OR
using the flight number as a key in the dict, and it's value as a dict with the other info.
What do you mean by
python Syntax (Toggle Plain Text)
d={"flight":T34712, From:ABERDEEN, scheduled 0800, remark landed}
The other keys need to be enclosed in quotes to, so that
python Syntax (Toggle Plain Text)
d['flight'] = 'T34712' d['from'] = 'ABERDEEN' #etc...
The best solution I think is paulthom12345's suggestion. Although, I'm still confused about the set-up you seem to want. There's a better way to organize it, such as one list with each index a separate flight, and each of those indices holding a dict with the keys for 'flight', 'from', 'scheduled', 'remark', etc. Like:
python Syntax (Toggle Plain Text)
flights = [ { 'flight': 'T34712', 'from': 'ABERDEEN', 'scheduled': 0800, 'remark': 'LANDED 08:00 ' }, { 'flight': 'BE171', 'from': 'SOUTHAMPTON', 'scheduled': 0820, 'remark': 'LANDED 08:07 ' } ]
python Syntax (Toggle Plain Text)
flights = { 'T34712': { 'from': 'ABERDEEN', 'scheduled': 0800, 'remark': 'LANDED 08:00 ' }, 'BE171': { 'from': 'SOUTHAMPTON', 'scheduled': 0820, 'remark': 'LANDED 08:07 ' } ]
Last edited by shadwickman; Mar 27th, 2009 at 7:32 pm.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
- Hunter S. Thompson
If I understand you right, it should look like this ...
python Syntax (Toggle Plain Text)
# assumed test data (text from a data file) data_str = """\ T34712 ABERDEEN 0800 LANDED 08:00 BE171 SOUTHAMPTON 0820 LANDED 08:07""" # first create a list of lists data_list = [line.split() for line in data_str.split('\n')] print data_list """ my prettied up result --> [ ['T34712', 'ABERDEEN', '0800', 'LANDED', '08:00'], ['BE171', 'SOUTHAMPTON', '0820', 'LANDED', '08:07'] ] """ # now create a list of dictionaries dict_list = [] data_dict = {} for line in data_list: sf = "%s, From:%s, scheduled %s, remark %s" value = sf % (line[0], line[1], line[2], line[3]) data_dict['flight'] = value dict_list.append(data_dict) # start dictionary over data_dict = {} print(dict_list) """ my prettied up result --> [ {'flight': 'T34712, From:ABERDEEN, scheduled 0800, remark LANDED'}, {'flight': 'BE171, From:SOUTHAMPTON, scheduled 0820, remark LANDED'} ] """
May 'the Google' be with you!
•
•
Join Date: Mar 2009
Posts: 10
Reputation:
Solved Threads: 0
sorry for the confusion guys
basically i want to read the test.txt data into a dictionary
d={"flight":BE171 , From: SOUTHAMPTON, scheduled 0820, remark landed}
where d is the dictionary
so that in future i can search thy dictionary by imputting BE171 etc hich outputs that key
does this make sense.
ive got something to work with now. ill post back when i have implemented the solution.
looks like i have to work on my making sense skills.
basically i want to read the test.txt data into a dictionary
d={"flight":BE171 , From: SOUTHAMPTON, scheduled 0820, remark landed}
where d is the dictionary
so that in future i can search thy dictionary by imputting BE171 etc hich outputs that key
does this make sense.
ive got something to work with now. ill post back when i have implemented the solution.
looks like i have to work on my making sense skills.
Last edited by lilkid; Mar 27th, 2009 at 7:49 pm.
![]() |
Similar Threads
- Replace words in a file (Python)
- File opening error (Python)
- Dictionary ADT (Computer Science)
- how to go over the text file sequantioly to get all (two) words from file?PLZ help (Python)
- Chinese Dictionary (Python)
- sorting a text file (C++)
- Concatenating dictionary values and keys, isnt working :( (Python)
- Reading from a file question (Python)
- getting words from .txt file (C++)
Other Threads in the Python Forum
- Previous Thread: getting errors
- Next Thread: I can't open Python GUI
Views: 1614 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
application array beginner c++ c/c++ change character class client code command convert count create csv ctypes curved database dictionary django dll error examples excel exe extensions fdlib file float format framework ftp function graphics gui homework image images import input library line linux list lists logging loop loops microcontroller mouse mysql mysqldb number numbers output parse parsing path port prime processing program programming py2exe pygame pygtk pyqt python random raw_input recursion recursive redirect remote scrolledtext serial server socket ssh stdout string strings syntax table terminal text thread threading tkinter transparency tuple tutorial ubuntu unicode variable variables web windows wxpython xml






