Results 1 to 5 of 5
Thread: String Literals
- 11-09-08, 02:06 AM #1
String Literals
ive been screwing with this for 3 hours now:
Ive got file x:
i want to remove"clients.txt"
{
"version" "1"
"players"
{
with python"version" "1"
string literal for that line SHOULD be:
but its not...Code:\n\t"version"\t\t"1"
works but leaves the new line... which defeats the purpose.Code:\t"version"\t\t"1"
\n removes the line
but when combined with the rest \t"version"\t\t"1"
fails
thoughts?
- 11-09-08, 11:03 AM #2
Re: String Literals
It really depends on how your using python to read the file. Could you give more context?
- 11-09-08, 12:59 PM #3
Re: String Literals
code using to remove lines:
Example Clients file:Code:import os import sys import fileinput textToSearchFor = '{\n\t"version"\t\t"1"' textToReplaceWith = '{' fileToSearch = 'clients.txt' oldFileName = 'old_' + fileToSearch tempFileName = 'temp_' + fileToSearch # If there's already an 'old_' prefixed backup file there from # a previous run, remove it... if os.path.isfile( oldFileName ): os.remove( oldFileName ) tempFile = open( tempFileName, 'w' ) for line in fileinput.input( fileToSearch ): tempFile.write( line.replace( textToSearchFor, textToReplaceWith ) ) tempFile.close() # Rename the original file by prefixing it with 'old_' os.rename( fileToSearch, oldFileName ) # Rename the temporary file to what the original was named... os.rename( tempFileName, fileToSearch )
Clients file with correct lines removed: .Code:"clients.txt" { "version" "1" // This key group lists all your client players "players" { // This must be a unique client name "...X..." { // Client real name "name" "...XXXX..." // Steam ID for client "steam" "STEAM_0:X:XXXXX" // These are personal access flags for a player "flags" { "Immunity" "b" } "groups" { "Admin" "01" } } // This must be a unique client name "...Y..." { // Client real name "name" "...YYYY..." // Steam ID for client "steam" "STEAM_0:Y:YYYY" // These are personal access flags for a player "flags" { "Immunity" "b" } "groups" { "Admin" "01" } } } // These are global groups of flags that can be assigned to clients "groups" { "Admin" { "01" "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d" "01" "e f g i k l m o p q r s t v w x y z q2 q3 admin spray grav" "01" "pban client" } } }
"clients.txt"
{
"version" "1" <-removed
// This key group lists all your client players <-removed
"players" <-removed
{ <-removed
// This must be a unique client name
"...X..."
{
// Client real name
"name" "...XXXX..."
// Steam ID for client
"steam" "STEAM_0:X:XXXXX"
// These are personal access flags for a player
"flags"
{
"Immunity" "b"
}
"groups"
{
"Admin" "01"
}
}
// This must be a unique client name
"...Y..."
{
// Client real name
"name" "...YYYY..."
// Steam ID for client
"steam" "STEAM_0:Y:YYYY"
// These are personal access flags for a player
"flags"
{
"Immunity" "b"
}
"groups"
{
"Admin" "01"
}
}
}
// These are global groups of flags that can be assigned to clients
"groups"
{
"Admin"
{
"01" "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d"
"01" "e f g i k l m o p q r s t v w x y z q2 q3 admin spray grav"
"01" "pban client"
}
} <-removed
}
- 11-09-08, 01:36 PM #4
Re: String Literals
Your processing the file one line at a time, which is why your text to replace which includes a newline doesn't match (when you process a file one line at a time, it breaks the text on the newline and doesn't include the newline, hence the lack of a match).
Instead of:
Try something more along the lines of:Code:for line in fileinput.input( fileToSearch ): tempFile.write( line.replace( textToSearchFor, textToReplaceWith ) )
Code:# Read in the entire file as one string contents = open(fileToSearch).read(); tempFile.write(contents.replace(textToSearchFor, textToReplaceWith));
- 11-09-08, 04:38 PM #5
Re: String Literals
yep that did it lol, thank you thank you...
Originally Posted by Ewok
Last time i work on something at 5am
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)


LinkBack URL
About LinkBacks






Reply With Quote

















Bookmarks