学习Python,其他项目的脚本

时间:2012-10-13 15:28:45

标签: python

问题:我的旧脚本在Python 3.x上无法解决问题

Offtopic;在尝试访问二进制和文本文件以进行大规模重命名,重新编号时,Python有多灵活?在Collision和IMG档案中?

我不再对此有最好的理解,因为我已经使用3dsmax走向了Level设计的方向。

无论如何..

错误:

Traceback (most recent call last):
  File "C:\SOL_REM.py", line 26, in <module>
    process_ide(sys.argv[1], int(sys.argv[2]),
  File "C:\SOL_REM.py", line 18, in process_ide
    ide_line = reduce(lambda x,y: str(x)+","+st
NameError: global name 'reduce' is not defined

代码:

import sys

if len(sys.argv) < 4:
    sys.exit('Usage: Source ide | ID number | Dest ide filename.' sys.argv[0])

def process_ide(ide_source, num, ide_destination):
    src = open(ide_source,'r')
    dst = open(ide_destination,'w')

    for line in src:
        ide_line = line

        if not (line == "" or line[0]=="#" or len(line.split(",")) < 2):
            ide_line = line.split(",")
            ide_line[-1] = ide_line[-1][:-2]
            ide_line[0] = num
            num+=1
            ide_line = reduce(lambda x,y: str(x)+","+str(y), ide_line)+"\n"

        dst.write(ide_line)

    src.close()
    dst.close()


process_ide(sys.argv[1], int(sys.argv[2]), sys.argv[3])

简单起见:

我要做的是通过以枚举顺序更改数字来解析ide文本文件。

语法将是SOL_rem.py game.ide 1845 game2.ide

示例文件:

ID    Modelname     TexName       Rendering flags.

objs
1700, ap_booth2_03, ap_airstuff1, 1, 190, 0
1701, ap_seaplaland1, ap_seasplane, 1, 299, 0
1702, ap_seaplanehanger1, ap_seasplane, 1, 299, 0
1703, ap_termwindows1, ap_termwindows, 1, 299, 4
1704, ap_blastdef_01, ap_newprops1opac, 1, 299, 4
1705, ap_blastdef_03, ap_newprops1opac, 1, 299, 4
1706, air_brway_030, airgrndb, 1, 299, 0
end

ID将从1845年按升序重新调整。

2 个答案:

答案 0 :(得分:2)

reduce不再位于Python 3的内置命名空间中。

为什么不使用reduce

,而不是使用join
ide_line = ','.join(ide_line) + '\n'

答案 1 :(得分:0)

在Python3中,你可以做到

from functools import reduce

即使在Python2.6 +中,上面应该没问题,但不是必需的。

是。无论你想做什么,Python都是完全灵活的。就像压倒内置组件一样。