简体   繁体   中英

Replacing text in a .nfo file

I have a colors.nfo file where I want to replace variables and get a new generated nfo-file without losing the template.

There are ascii-signs which I don't know how to handle. Every time I load with file.open and replace the variables and write it to a new file, there are weird signs and the template is destroyed.

Here is an image of the file: https://i.imgur.com/8lqqXpg.png

Here is the uploaded file to handle with: Click to download -- Hope its okay. otherwise i will delete!

Hope you understand the problem. Want to replace the "%REPLACE1%", "%REPLACE2%" and "%REPLACE3%" with for example "BLACKGREY", "REDWHITE"....

  • Tried to load it in a string with f.open
  • after I replaced it with string.replace("%REPLACE1", "BLACKGREY")
  • after I write a new file with f.write
  • file is destroyed and the ascii signs are unreadable and the template is not like before

Code Example:

replaceString = []
f = open("colors.nfo")
for line in f:
    replaceString.append(line.rstrip())
f.close()
replaceColors = "\n".join(replaceString)
print(replaceColors.replace("%REPLACE1%", "BLACKGREY"))

Output:

ÛÛ³    [x] Yellow      [ ] Yellow       [ ] Yellow      ³ÛÛ
ÛÛ³    [x] Pink        [ ] Pink         [ ] %REPLACE3%  ³ÛÛ
ÛÛ³    [ ] Green       [ ] green        [ ] Green       ³ÛÛ
ÛÛ³    [ ] Red         [ ] red          [ ] Red         ³ÛÛ
ÛÛ³    [ ] Blue        [ ] blue         [ ] Blue        ³ÛÛ
ÛÛ³    [ ] Black       [ ] %REPLACE2%   [ ] black       ³ÛÛ
ÛÛ³    [ ] White       [ ] white        [ ] white       ³ÛÛ
ÛÛ³    [ ] grey        [ ] grey         [ ] grey        ³ÛÛ
ÛÛ³    [ ] brown       [ ] brown        [ ] brown       ³ÛÛ
ÛÛ³    [ ] BLACKGREY  [ ] orange       [ ] orange      ³ÛÛ
ÛÛ³    [ ] purple      [ ] purple       [ ] purple      ³ÛÛ

How it should be:

██│    [x] Yellow      [ ] Yellow       [ ] Yellow      │██
██│    [x] Pink        [ ] Pink         [ ] %REPLACE3%  │██
██│    [ ] Green       [ ] green        [ ] Green       │██
██│    [ ] Red         [ ] red          [ ] Red         │██
██│    [ ] Blue        [ ] blue         [ ] Blue        │██
██│    [ ] Black       [ ] %REPLACE2%   [ ] black       │██
██│    [ ] White       [ ] white        [ ] white       │██
██│    [ ] grey        [ ] grey         [ ] grey        │██
██│    [ ] brown       [ ] brown        [ ] brown       │██
██│    [ ] BLACKGREY   [ ] orange       [ ] orange      │██
██│    [ ] purple      [ ] purple       [ ] purple      │██

I don't want these "ÛÛ" in my new created file. I want to have the "blackboxes" like in the screen. Replacing is not the problem. Problem is the structure after loading the file into a string. when I write these string to a new file, the template does not look like in the screen shown.

There are two problems here

  1. reading and writing the data correctly
  2. preserving the structure

Reading and Writing

According to Wikipedia , .nfo files of this type are encoded with the cp437 text encoding. Therefore this encoding must be specified when reading and writing the file.

with open('colors.nfo', 'r', encoding='cp437') as f:
    ...

with open('colors.nfo', 'w', encoding='cp437') as f:
    ...

Alternatively, the file may be opened in binary mode, and all operations carried out using bytes rather than text.

with open('colors.nfo', 'rb') as f:
    ...

If no encoding is specified, Python uses the system default, probably cp1252 in this case.An 8-bit encoding like cp1252 is able to decode the file, but where cp437 decodes b'\xdb' as 'FULL BLOCK' (█), cp1252 decodes it as 'LATIN CAPITAL LETTER U WITH CIRCUMFLEX' (Û), corrupting the data as seen in the question.

Preserving Structure

The data has a fixed-width format, so care must be taken when replacing text that the target and replacement strings are both of equal length, otherwise the columns will not be aligned correctly.

target = '%REPLACE1%'
replacement = 'BLACKGREY'

delta = len(target) - len(replacement)

padding = ' ' * abs(delta)
if delta > 0:
    replacement += padding
else:
    target += padding

Solutions

Here's a complete script.

replacements = { 
        '%REPLACE1%': 'BLACKGREY',
        '%REPLACE2%': 'REDWHITE',
}   

with open('colors.nfo', encoding='cp437') as f:
    data = f.read()

for target, replacement in replacements.items():
    delta = len(target) - len(replacement)
    padding = ' ' * abs(delta)
    if delta > 0:
        replacement += padding
    else:
        target += padding

    data = data.replace(target, replacement)

with open('new-colors.nfo', 'w', encoding='cp437') as f:
    f.write(data)

Processing as binary data is the same, except that the files are opened and closed in binary mode, and strings are declared as bytes rather than str .

replacements = { 
        b'%REPLACE1%': b'BLACKGREY',
        b'%REPLACE2%': b'REDWHITE',
}   

with open('colors.nfo', 'rb') as f:
    data = f.read()

for target, replacement in replacements.items():
    delta = len(target) - len(replacement)
    padding = b' ' * abs(delta)
    if delta > 0:
        replacement += padding
    else:
        target += padding

    data = data.replace(target, replacement)

with open('new-colors.nfo', 'wb') as f:
    f.write(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM