#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    12
    Rep Power
    0

    Editing a name in a log


    hi all

    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:
    f2.write(line)

    so to find it = if "^1[-R^7o^43-t]^7-Cereal Killer-" in line:
    code to remove the t
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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…

    Code:
    #!/usr/bin/env python
    
    path = 'Sample.txt'
    
    lines = [line.replace('[-R^7o^43-t]', '[-R^7o^43-]') for line in file(path)]
    file(path, 'w').writelines(lines)
    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.

    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,

    Mark.
    Last edited by netytan; February 6th, 2005 at 03:14 PM.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    12
    Rep Power
    0

    Thumbs up


    Originally Posted by netytan
    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…

    Code:
    #!/usr/bin/env python
    
    path = 'Sample.txt'
    
    lines = [line.replace('[-R^7o^43-t]', '[-R^7o^43-]') for line in file(path)]
    file(path, 'w').writelines(lines)
    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.

    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,

    Mark.

    Nice thanx

    i got it to work
    super

    Thanks again

IMN logo majestic logo threadwatch logo seochat tools logo