The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Page 2 -
Newbie read .txt file question.
Page 2 - Discuss Newbie read .txt file question. in the Python Programming forum on Dev Shed. Newbie read .txt file question. Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 20th, 2012, 11:19 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 12
Time spent in forums: 5 h 24 m 39 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Scorpions4ever ANSI file?? What exactly are you using to edit your files? That might help explain what's going on. |
Sorry for causing this confusion. I will give more details. The ANSI file is where the data is (test.dat). The data is from a HMTL page. I copy the page manually and paste it into notepad and save it as a text file called test.dat. I hope that helps you understand how I'm saving the data and causing possible errors on reading.
I've included links to the dat file and the Python file I'm using if that helps any further.
https://dl.dropbox.com/u/35719977/test.dat
https://dl.dropbox.com/u/35719977/tester1.py
|

December 20th, 2012, 03:51 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Your test.dat already has [ and ] in it, so I merely modified tester1.py to look more like my original program:
Code:
#!/usr/bin/python
import json
#import pprint
fp = open('test.dat', 'r')
json_obj = json.load(fp)
fp.close()
for record in json_obj:
print(record[u'name'] + " " + record[u'age'])
#pprint.pprint(record)
Note that you had not imported pprint, so I fixed that as well and the pprint.pprint line is commented out as well. The output is:
Code:
Mahmoud El-Shazly 32.00
Bobby Roberts 30.08
Józef Barna 20.10
Peter Shelly 32.02
Jamie Underwood 33.00
Ma PeiSi 32.00
Chris Taylor 31.09
Rodolfo Escribano 31.04
Joshua Sidgwick 20.08
Dale Dudley 31.09
Dave Thompson 22.09
Steven Piper 23.03
Robbie Stevenson 20.09
Stu Phillipson 29.01
Robbie Webster 28.06
Michael Downing 26.01
Zbigniew Kas 21.05
Scott Richards 31.03
Thomas Davies 26.11
Paul Stevens 31.04
Phillip Wilson 17.02
Alex Mullen 18.04
Lee Lineker 20.08
Tony Felton 18.05
Stephen Boyham 20.09
Steven Ludlow 20.11
Mihails Solovjovs 20.05
Vladimer Borodin 20.08
Jelle Mensonides 20.10
Laokratis Pavlidis 22.08
Osyp Stukach 20.11
Sergiu Iencsi 20.08
Matt Ackland 16.03
Geoff Smith 27.01
Richard Marshall 19.10
Branko Gagić 20.04
Oliver Baker 20.11
Boaz bin Amran 20.06
Giovani Mirouf 20.10
Rich Dawson 20.06
Brian Twiss 22.05
Stuart Nugent 22.11
Howard Gerrard 17.08
Stuart Greaves 20.00
Tony Taylor 18.04
Brian Thorne 17.11
Anthony Baxter 19.05
Billy Crouch 18.03
Bradley Pierce 22.00
Forrest Phillips 19.10
Robert Jones 17.07
Will Lampard 18.10
Simon Thomas 17.04
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

December 21st, 2012, 04:29 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 12
Time spent in forums: 5 h 24 m 39 sec
Reputation Power: 0
|
|
|
Scorpions4ever thank you so much for the patience,time and knowledge you have given me I really appreciate it. Merry Christmas.
Would I be able to use this method to write to SQL database? I'm not attempting this yet as I need to learn more about Python as my knowledge is very poor but for future reference it may be helpful.
|

December 21st, 2012, 11:49 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 8
Time spent in forums: 4 h 41 m 11 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by skyblues Scorpions4ever thank you so much for the patience,time and knowledge you have given me I really appreciate it. Merry Christmas.
Would I be able to use this method to write to SQL database? I'm not attempting this yet as I need to learn more about Python as my knowledge is very poor but for future reference it may be helpful. |
JSON is like a JAVA version of the XML format so it would certainly be fine for database use. A bit of overkill perhaps since you often desire to keep the parts of a database record separate so you can do queries and other DB operations without parsing a complex file format.
If you want to know more look into the python module called json.
docs.python.org/2/library/json.html
|

December 21st, 2012, 04:27 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 12
Time spent in forums: 5 h 24 m 39 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Marbelous JSON is like a JAVA version of the XML format so it would certainly be fine for database use. A bit of overkill perhaps since you often desire to keep the parts of a database record separate so you can do queries and other DB operations without parsing a complex file format.
If you want to know more look into the python module called json.
docs.python.org/2/library/json.html |
Thank you Marbelous for the information and link. I'll take a look and hopefully learn more about this module.
|

December 21st, 2012, 07:23 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Quote: | Originally Posted by skyblues Scorpions4ever thank you so much for the patience,time and knowledge you have given me I really appreciate it. Merry Christmas.
Would I be able to use this method to write to SQL database? I'm not attempting this yet as I need to learn more about Python as my knowledge is very poor but for future reference it may be helpful. |
The python json library is meant to read/write JSON files. However, there are other modules to work with databases in python. Since you're using Python on Windows (I'm guessing this, since you mentioned notepad earlier), I wrote a tutorial a while ago on using python with ADO to work with databases. Hope you find it useful.
|

December 22nd, 2012, 03:11 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 12
Time spent in forums: 5 h 24 m 39 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Scorpions4ever The python json library is meant to read/write JSON files. However, there are other modules to work with databases in python. Since you're using Python on Windows (I'm guessing this, since you mentioned notepad earlier), I wrote a tutorial a while ago on using python with ADO to work with databases. Hope you find it useful. |
Thank you once again Scorpions4ever.
Yes you have guessed correctly I'm am using Windows.
I will take a more detailed look at your tutorial and I'm sure I'll find it very useful.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|