Click to See Complete Forum and Search --> : and another python question
Ludootje
01-21-2002, 05:10 PM
i already checked ccae and a tutorial, but it's always so damn difficult. is there no easy & logical way to do this?
i want python to write a variable to a file, and if possible:
- create the file (though optional)
- i'd like to tell it on which line the variable should be written, if that's not possible/incredibly difficult, the variable should be written below the last line where something is typed.
http://python.org/doc/current/lib/bltin-file-objects.html
here's some info on using files in python.
You can't specify a line number; you'd have to read in all the lines (easy enough-> use readlines()) and then add the new line wherever you want to in the list of lines.
so, you open the file for reading, then modify the data. then open it for writing and write it out.
There is an example at CCAE written by inkedmn (under Brett Kelly at CCAE) about file opening, reading, and closing. Just do a search in Python for 'file'... I suggest you look at that before writing your own code.
actually, there are two good examples there, the other one is by Ben Briggs.
Gnu/Vince
01-21-2002, 06:16 PM
I have a Python question of my own too: is there a chomp-like operator in Python? I know about splitlines(), but it returns a list. So right now I created a small module called chomp:
#!/usr/bin/env python
from string import join
def chomp(s):
return join(s.splitlines())
Is there something equivalent in the core libraries?
Also, I want to congratulate the Python developpement team. Recently they unified short ints and long ints. I don't know if it's the reason, but Python now works much faster with long integers. I once posted something in which Ruby and Python calculated the factorial of 65,536; Ruby took about 4:30 minutes and Python 2.1 took over 12 minutes. Here are the new results for Python 2.2:
[vince@vincent: ~/prog]% time ruby/fact.rb 65536 > /dev/null
ruby/fact.rb 65536 > /dev/null *261.95s user 0.94s system 99% cpu 4:23.70 total
[vince@vincent: ~/prog]% time python/fact.py 65536 > /dev/null
python/fact.py 65536 > /dev/null *214.33s user 5.43s system 99% cpu 3:40.28 total
jemfinch
01-22-2002, 07:53 AM
Originally posted by Gnu/Vince:
I have a Python question of my own too: is there a chomp-like operator in Python? I know about splitlines(), but it returns a list. So right now I created a small module called chomp:
Just use string slicing.
s = "This ends in a newline.\n"
without_newline = s[:-1]
Jeremy
Gnu/Vince
01-22-2002, 08:49 AM
Originally posted by jemfinch:
<STRONG>Just use string slicing.
s = "This ends in a newline.\n"
without_newline = s[:-1]
Jeremy</STRONG>
I heard that splitlines() was more crossplatform, because it also remove "\r\n" (Windows I think) and that it works with Unicode.
[ 22 January 2002: Message edited by: Gnu/Vince ]
inkedmn
01-22-2002, 12:48 PM
Originally posted by kmj:
<STRONG>There is an example at CCAE written by inkedmn...</STRONG>
wow, i never though anybody'd ever pimp my code. ;) :D
Ludootje
01-22-2002, 12:49 PM
Originally posted by kmj:
<STRONG>http://python.org/doc/current/lib/bltin-file-objects.html
here's some info on using files in python.
You can't specify a line number; you'd have to read in all the lines (easy enough-> use readlines()) and then add the new line wherever you want to in the list of lines.
so, you open the file for reading, then modify the data. then open it for writing and write it out.
There is an example at CCAE written by inkedmn (under Brett Kelly at CCAE) about file opening, reading, and closing. Just do a search in Python for 'file'... I suggest you look at that before writing your own code.</STRONG>
i did, and that's why i specify is there no easy & logical way to do this?
:D
with easy&logical i mean something of this kind:
writeln /home/ludo/filetowriteto.txt -l12 "this is what i want to write"
with "-l12" being the line of course :)
aaaah mirc scripting.. isn't that just so incredibly easy?
edit: ow, and since you speak about it, i saw the one from ben briggs too..
[ 22 January 2002: Message edited by: Ludootje ]
Originally posted by Ludootje:
<STRONG>
with easy&logical i mean something of this kind:
writeln /home/ludo/filetowriteto.txt -l12 "this is what i want to write"
with "-l12" being the line of course :)
aaaah mirc scripting.. isn't that just so incredibly easy?
edit: ow, and since you speak about it, i saw the one from ben briggs too..
</STRONG>
I don't think there is, since any time you write to a file, you really have to read the whole thing in, write to your specified location and write the whole thing back out.
If mirc script wraps all that work in a single function, that's nice; you could make your own utility function to do that in python if you'd like. Something like:
# insert linestr before linenum of filestr;
# assumes linestr already ends with '\n'
def fwrite_line(filestr, linestr, linenum):
file = open(filestr, 'r')
lines = file.readlines()
file.close()
lines[linenum] = linestr + lines[linenum]
file = open(filestr, 'w')
file.writelines(lines)
file.close()
That may not be the most efficient way to do it, but something like that should work.