python 3 IO

trying to figure out the syntax to find and replace something in a text file via python 3.

i have figured out how to open the file, read, append/extend, and close. but i cannot find anything anywhere that explains how to find something (say ‘item1’) and then remove or edit that particular piece of text.

You didnt respond to my last post.

what kind of information are you writing to a file? did you do any of those exercises suggested by the google pyquick lectures? (granted they are based on an older version of python…for the sake of learning python that is, however, less than relevant than doing them) The lectures cover regular expression and string matching ( http://code.google.com/edu/languages/google-python-class/regular-expressions.html )

The lecturer Nick Parlante works at Stanford and Google and I think he described programming python in a python style like “living higher up on the food chain” .

Content has been removed. MayDev

it’s just a regular text.txt file meant to hold information that can be grabbed and edited by various scripts.

Zeffi: i have read a number of python 3 tutorials over the last few days. the concept of the language isn’t my problem. it’s syntax. i know enough other languages to know how to program, but the actual syntax is difficult. i have yet to find anything in any link or site (posted at blenderartists.org or otherwise) that explain find and editing a specific line within a document.
from what i understand, python 3 is very limited as far as anything very global goes. i believe that’s why this is more difficult than i initially expected. i’ve never worked with a language that had such a problem.

Content has been removed. MayDev

As you suggest already your difficulty is python, and not blender, perhaps spend a little bit more time exploring the goodies that python has to offer, and when you are comfortable with the syntax and modules then programming for blender will be a breeze.

but do as mayhemofhell suggest, elaborate on what you want to achieve a little more… no one is interested in coding for you, coding is too much fun to get others to do it… but we can possibly shed some light on whether or not you own proposed approach is the best for python. The longer you can avoid writing to disk the better. (Lists and Dictionaries have vastly more rapid lookup times than I/O streams )

Content has been removed. MayDev

oh, that’s a good idea.

what i’m actually working on is an inventory for my game. the initial script upon starting the game creates an inv.txt file.

example:

file = open("inv.txt", "w")
file.write("test")
file.write("
")
file.close()

generally it’d be empty, but the ‘test’ text is there because i want to be able to find and edit it in the next script.

there would be a script for when an item is picked up. say, a gun.


file = open("inv.txt", "a")
file.write("gun")
file.write("
")
file.close()

works well enough. then the next script (the problem script) is run if, say, something happens to remove the item ‘test’ from the inventory.

pseudo code:


file = open("inv.txt", "a")
find "test"  (first line found with the word used)
remove "test" (where found)
file.close()

i’m familiar with php and using it with mysql, so i know how to pull mysql info into php, edit it, and resave it. or, which i’d prefer to do with python 3, edit the content directly (such as when using php to edit via mysql syntaxes).

i’d like to do something similar.

i can’t use a dictionary because it only works within a single script. i attempted the use of global variables, but python 3 isn’t recognizing any (even if assigned as global) within a separate script.

i realize learning python 3 is of course important, but i cannot learn the syntax if i can’t find it, or examples of it. yes, i’ve read every link you’ve provided Zeff, but none of them are showing anything like what i need to do. not that i can find. learning python 3 isn’t really an issue. i can learn it pretty fast because thus far it’s a very basic, simple language. the problem is the syntax. how to find the appropriate terms to use in order to say “find this in the file” and “edit this in the file”.

Content has been removed. MayDev

that is perfect! thanks! it’s easily understood and manipulatable. exactly what i need. much appreciated, and if you don’t mind, i plan to make a tutorial later on bge inventories and will likely use this template, credits to you for the help.

Content has been removed. MayDev

sure thing, that’d be great. it’s mainly because there are SO many people asking for how to make an inventory (the bge forum, for example) and currently absolutely no up to date tutorials or even examples seem to exist. they aren’t -horribly- hard (though i may be wrong, given python 3 isn’t my area of expertise) so i think a good one ought to exist. might inspire people to start learning script as well. drop me a message if you’d like.

Content has been removed. MayDev

well, if you’d like i can send you the blendfile i currently have for my own game (messy, since i’m still very much experimenting with things) and you can see the items there. it’s a fairly simple first level, but has the basics. there’s a gun you can pick up, a medicine box, half-assed inventory (items yet to be actually displayed, but it works ishly) and a key and bobby pin to open doors with. think those are the basics. it actually works alright when using logic bricks, but i want to improve it now that i feel i have the game designing concept down. it’d be interesting to see the same scene used via python scripting. another thing is, logic bricks don’t carry info to the second level, aka scene. scripts do.

i also am tinkering a bit with the script you gave me, trying out some things. anyhow, here’s the blendfile.

http://www.mediafire.com/?rd67i8z22ebmiw3

i personally got into coding because i wanted to make some games of my own. learned most everything i know via tutorials and picking apart examples of things other people had made, so i figure, might as well offer the same to others.

this is also a fairly basic and slightly altered version of your script:

http://www.mediafire.com/?5aedg6s96ixaasc

And those values are going to be the keys for a dictionary that actually contains the information about the items?

Personally, whenever i read from a text file, I store the data in a buffer first. Then i normally use a variable.find(“search text”) to give me an index. And yeah, i go from there.

You’ll probably want some sort of method for storing multiple items of the same type. Probably just add a number to the end of the item key, separated by a space. Separated by a space so you can use something like variable.split() to split the string into a dictionary containing two strings, which were on other sides of the space (if you use any other symbol, eg a colon, just put “:” in split())

Content has been removed. MayDev

From talking with people on freenode irc, with the folks on #python, http://www.postgresql.org/, was the preferred database backend.

Personally, I think the only problem with a text based file is the somewhat longish time to read the data into the computer. If you don’t have that many items, keeping the item data in memory and reading it in the first place shouldn’t be a problem. If you have lots, a database would probably be preferable. I don’t know what kind of state the database libraries are in for python 3x.

This is something i’ve done previously for python 2x.

Sorry if the last two links don’t work, sourceforge beta is kinda weird.

Content has been removed. MayDev

an actual database would definitely work better, especially something like sql. from what i’ve read though, python doesn’t like accessing anything external and it tends to take some time. might have changed between 2 and 3. i figured a simple text file would be the least exhaustive to the script.

databases are more efficient, so if that were possible i’d personally go for it. i wonder though if most other bge users would be able to figure out how to set it all up, or be willing? like i said, i’d use a database, but then i have experience with databases. specifically php to mysql. gotta love that stuff.

hey Fake, you ever play a MUD before? that map reminds me of 'em.

Content has been removed. MayDev