Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7-在Win7和OSX上写入文件时的差异_Python_Windows_Macos_Python 2.7 - Fatal编程技术网

Python 2.7-在Win7和OSX上写入文件时的差异

Python 2.7-在Win7和OSX上写入文件时的差异,python,windows,macos,python-2.7,Python,Windows,Macos,Python 2.7,我一直在写一个程序,读取一个文件,并在再次写入之前更改特定字符。当我在OSX下运行程序时,输出完全符合我的要求。但是,当尝试在Windows下运行时,写入的文件中会出现大量意外字符。如果我在编写Python之前检查输出的长度,它就是预期的大小,因此我假设Windows上的Python编写协议有所不同。如果您感兴趣,这里有一段代码 rom = open(rom_name, 'rb').read() rom_list = list(rom) for item in abili

我一直在写一个程序,读取一个文件,并在再次写入之前更改特定字符。当我在OSX下运行程序时,输出完全符合我的要求。但是,当尝试在Windows下运行时,写入的文件中会出现大量意外字符。如果我在编写Python之前检查输出的长度,它就是预期的大小,因此我假设Windows上的Python编写协议有所不同。如果您感兴趣,这里有一段代码

    rom = open(rom_name, 'rb').read()
    rom_list = list(rom)
    for item in ability_locations:
        address = int(item, 16)
        rand_ind = random.randint(0,len(ability_values) - 1)
        new_enemy = ability_values[rand_ind]
        new_enemy = chr(int(new_enemy,16))
        rom_list[address] = new_enemy
    rom = "".join(rom_list)
    new_rom = open(rom_name.split(".")[0] + "_" + str(KA_seed) + ".nes", 'w')
    new_rom.write(rom)
    new_rom.close()
可能值得注意的是,我正试图修改一个十六进制文件,所以许多字符是“不寻常的”。我不知道写这些字是否有问题

如果你能给我任何帮助,我将不胜感激。谢谢


编辑:对于将来有相同问题的人,以二进制模式写入修复了我的问题(“wb”而不是“w”)。

Windows在以文本模式写入时插入carrage返回字符。以二进制模式写入文件
“wb”


在文本模式下写入时,Windows会插入carrage返回字符。以二进制模式写入文件
“wb”


以二进制模式编写
“wb”
。哇,是的,就是这样。非常感谢。以二进制模式编写
“wb”
。哇,是的,就是这样。非常感谢。它不是Windows本身,因为
ReadFile
WriteFile
使用二进制文件。这是C运行时在Windows上对文本模式的实现。它还将Ctrl+Z(即“
”\x1a“
)解释为EOF,在以文本模式读取文件时可能会让人感到惊讶。值得注意的是,Python 3不使用CRT的文本模式,也不模拟从CP/M继承的Ctrl+Z无意义(从控制台读取时除外)。它本身不是Windows,因为
ReadFile
WriteFile
使用二进制文件。这是C运行时在Windows上对文本模式的实现。它还将Ctrl+Z(即“
”\x1a“
)解释为EOF,在以文本模式读取文件时可能会让人感到惊讶。值得注意的是,Python3没有使用CRT的文本模式,也没有模拟从CP/M继承的Ctrl+Z模式(从控制台读取时除外)。
with open(rom_name, 'rb') as rom:
    rom = rom.read()
rom_list = list(rom)
for item in ability_locations:
    address = int(item, 16)
    new_enemy = random.choice(ability_values)
    rom_list[address] = chr(int(new_enemy, 16))
with open('{}_{}.new'.format(rom_name.split(".")[0], KA_seed), 'wb') as new_rom:
    new_rom.write("".join(rom_list))