February 6th, 2005, 12:27 PM
Editing a name in a log
i have a problem in my log and i want to change a username to another one, i only want to change 1 letter
i already got a log cleaner that searches every line
but how can i change a line like this:
29:25 D;377158;3;axis;-SPEEDY-;0;6;allies;^1[-R^7o^43-t]^7-Cereal Killer-;bren_mp;60;MOD_RIFLE_BULLET;torso_lower
into this :
29:25 D;377158;3;axis;-SPEEDY-;0;6;allies;^1[-R^7o^43-]^7-Cereal Killer-;bren_mp;60;MOD_RIFLE_BULLET;torso_lower
minus the t in [-Ro3-]
without changing the line?
i only need a way to change the first line into the second line
my cleaner code is :
# open the file to read from
f = open("games_mp.log", "r")
# open the file to write to
f2 = open("games_cleaned.log", "w")
# iterate over the lines of the read-file
for line in f.readlines():
if not "say" in line:
if not "Item" in line:
if not "-----" in line:
so to find it = if "^1[-R^7o^43-t]^7-Cereal Killer-" in line:
code to remove the t
February 6th, 2005, 02:52 PM
If thats all you need to do then your should be able to iterate over the lines in the file and replace [-R^7o^43-t] with [-R^7o^43-] using the replace() string method and write the file back out.
Edit: An example…
This code uses a list comprehension to preform the actual work on the file and assigns the results to the lines variable, which is then written back to the file.
path = 'Sample.txt'
lines = [line.replace('[-R^7o^43-t]', '[-R^7o^43-]') for line in file(path)]
My original (untested) approach was to use a generator expression inside writelines(), however this would only work if you gave the output file a different name to the input file so I decided to separate the two parts.
Another way to do this would be to send the reformatted lines to stdout and then use the command line to write them to a specify file – handy if you want to write command line programs, since the lines can be pipes to another program (on *nix anyway).
Hope this helps,
Last edited by netytan; February 6th, 2005 at 03:14 PM.
February 7th, 2005, 10:11 AM
Originally Posted by netytan
i got it to work