![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2007
Posts: 11
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Sexy Programmer
|
Try using XML.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Aug 2007
Posts: 11
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#4 |
|
Sexy Programmer
|
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...
}<card>
<name>Blue Eyes White Dragon</name>
<hitpoints>1202020</hitpoints>
<attacks>
<name>Fire Spin</name>
<name>Tail Whip</name>
</attacks>
</card>
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Aug 2007
Posts: 11
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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: python Syntax (Toggle Plain Text)
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.
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Aug 2007
Posts: 11
Rep Power: 0
![]() |
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. Last edited by Primusville; Aug 17th, 2007 at 4:24 AM. |
|
|
|
|
|
|
#8 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5
![]() |
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Aug 2007
Posts: 11
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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.
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problem processing file into a char array | csrocker101 | C++ | 1 | May 8th, 2007 11:50 PM |
| OnlineTextEditor.Com! | Sane | Show Off Your Open Source Projects | 43 | Jun 16th, 2006 8:55 AM |
| Problem read'ing from file and user input | jmsilver | Bash / Shell Scripting | 3 | May 20th, 2005 2:35 PM |
| After execution - Error cannot locate /Skin File? | wchar | Visual Basic | 1 | Mar 5th, 2005 9:04 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |