Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    25
    Rep Power
    0
    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
  2. #17
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    25
    Rep 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.
  6. #19
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Tewksbury, MA
    Posts
    37
    Rep Power
    2
    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
  8. #20
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    25
    Rep Power
    0
    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.
  10. #21
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    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.
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #22
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    25
    Rep Power
    0
    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.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo