Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Problem with file input (http://www.programmingforums.org/showthread.php?t=13773)

Primusville Aug 16th, 2007 1:17 AM

Problem with file input
 
Well, I recently got back into Yu-Gi-Oh cards (the show sucks, but the game is actually fun) as just another way to pass time. Anyway, it gave me the idea to write a Yu-Gi-Oh TCG game in Python. Just command line to start with, maybe with graphics later. My problem is, I want to use text files as databases to hold card information so that I can easily add new cards, but I can't think of a good way to input the file in Python and then make it so that each card adheres to a certain class, and is treated a a separate instance (unless there is a better way). How would I go about doing this?

Many thanks in advance.

ReggaetonKing Aug 16th, 2007 1:18 AM

Try using XML.

Primusville Aug 16th, 2007 1:20 AM

What could I do with XML that I couldn't do with a regular text file (well a lot, I'm sure, but I'm just speaking in the context of my game), and how would I implement the XML files into my program so that they adhered to a class like I want to? I don't have any previous experience with XML so I have no idea. Thanks for the suggestion.

Edit: Also, I need to make it so that the card addition process is completely automated. Would I use a for loop for this?

ReggaetonKing Aug 16th, 2007 1:40 AM

Imagine if you have a class, Card. This class has the following attributes:
:

class Card
{
        int hitPoints;
        string name;
        string[] attacks;
        ....functions and other shit goes here...
}

Now with the example above, you can read from a XML file, example:
:

<card>
  <name>Blue Eyes White Dragon</name>
  <hitpoints>1202020</hitpoints>
  <attacks>
      <name>Fire Spin</name>
      <name>Tail Whip</name>
  </attacks>
</card>

I do know a bit of Python but not enough to read from a XML and parse it to extract all of that information. I'm sure someone here could help with that. But if you see my two examples above, that could get your design started.

Primusville Aug 16th, 2007 1:47 AM

Thanks a million for that help; it's definitely what I needed to get started. I found a good guide on processing XML in Python, so that in conjunction with your post will help a lot.

Game_Ender Aug 17th, 2007 2:43 AM

You don't really have to use XML. You just need a structured data format you could use Json with the Python
simplejson library.

Here is a usage example:
:

  1. import simplejson
  2.  
  3. # file mycards.txt
  4. # {
  5. # "Blue Eyes White Dragon" : {
  6. #    "hitpoints"  : 1202020
  7. #    "attacks" :  ["Fire Spin", "Tail Whip"]
  8. #    }
  9. # }
  10.  
  11. mycards = file("mycards.txt")
  12. myData = simplejson.load(mycards.read())
  13. hitpoints = myData["Blue Eyes White Dragon"]["hitpoints"]


XML might be a good data interchange format, but its pretty verbose, and a pain to work with by hand compared to Json and Yaml. The key is, there is no "parsing" with Json or Yaml. They are already in dict + list format. The data structure in the file exactly maps to python. No hoop jumping or awkward APIs.

Primusville Aug 17th, 2007 4:31 AM

Quote:

Originally Posted by Game_Ender (Post 132326)
You don't really have to use XML. You just need a structured data format you could use Json with the Python
simplejson library.

Here is a usage example:
:

  1. import simplejson
  2.  
  3. # file mycards.txt
  4. # {
  5. # "Blue Eyes White Dragon" : {
  6. #    "hitpoints"  : 1202020
  7. #    "attacks" :  ["Fire Spin", "Tail Whip"]
  8. #    }
  9. # }
  10.  
  11. mycards = file("mycards.txt")
  12. myData = simplejson.load(mycards.read())
  13. hitpoints = myData["Blue Eyes White Dragon"]["hitpoints"]


XML might be a good data interchange format, but its pretty verbose, and a pain to work with by hand compared to Json and Yaml. The key is, there is no "parsing" with Json or Yaml. They are already in dict + list format. The data structure in the file exactly maps to python. No hoop jumping or awkward APIs.

Thank you! That's just what I was looking for. At first XML seemed like a great idea, but then I realized the unnecessary difficulty of parsing it in Python. Thanks again for the help; now I think that I can finally get to work on my game.

Edit: I installed simplejson, and it's completely PERFECT for what I need. It works just as well as I hoped it would. Thanks once again for your assistance.

lectricpharaoh Aug 17th, 2007 5:03 AM

I would be very surprised if there weren't libraries to do this in Python. I've never actually used the language, but XML is so ubiquitous that it seems silly to be lacking support.

Primusville Aug 17th, 2007 5:22 AM

There are. In fact, newer versions of Python include them by default. They're just not practical for what I need to do. Too much work to get something so simple done.

Game_Ender Aug 17th, 2007 3:36 PM

Python has 3 built in XML libraries but they are either SAX or DOM based. There is a recipe on the Python cookbook that enables a similar dict/list interface to xml data but the resulting xml, is again, very awkward. I think there might be few python modules that wrap these libraries to provide a dict type interface, but why use them? For many purposes JSON is good enough, its easier to type, and has good language support.


All times are GMT -5. The time now is 3:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC