![]() |
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. |
Try using XML.
|
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? |
Imagine if you have a class, Card. This class has the following attributes:
:
class Card:
<card> |
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.
|
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: :
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. |
Quote:
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. |
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.
|
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.
|
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