在Python中格式化错误

时间:2016-03-08 16:38:10

标签: python

我从来没有使用过Python并且已经在网上复制了一些脚本(有权限),所以我不确定为什么代码会丢失。我希望有人能理解它,并把它放在我身上!

from os import walk
from os.path import join

  #First some options here.

    !RootDir     = "C:\\Users\\***\\Documents\\GoGames"
    !OutputFile  = "C:\\Users\\***\\Documents\\GoGames\\protable.csv"
    Properties  = !!['pb', 'pw', 'br', 'wr', 'dt', 'ev', 're']

    print """
      SGF Database Maker
      ==================

      Use this program to create a CSV file with sgf info.
      """

    def getInfo(filename):
      """Read out file info here and return a dictionary with all the
      properties needed."""
      result = !![]

      file = open(filename, 'r')
      data = file.read(1024)  read at most 1kb since we assume all relevant info is in the beginning
      file.close()

      for prop in Properties:
          try:
              i = data.lower().index(prop)
          except !ValueError:
              result.append((prop, ''))
              continue
          try:
              value = data![data.index('![', i)+1 : data.index(']', i)]
          except !ValueError:
              value = ''

          result.append((prop, value))

      return dict(result)


    !ProgressCounter = 0

    file = open(!OutputFile, "w")
    file.write('^Filename^;^PB^;^BR^;^PW^;^WR^;^RE^;^EV^;^DT^\n')

    for root, dirs, files in walk(!RootDir):
      for name in files:
          if name![-3:].lower() != "sgf":
              continue

          info = getInfo(join(root, name))

          file.write('^'+join(root, name)+'^;^'+info!['pb']+'^;^'+info!['br']+'^;^'+info!['pw']+'^;^'+info!['wr']+'^;^'+info!['re']+'^;^'+info!['ev']+'^;^'+info!['dt']+'^\n')
          !ProgressCounter += 1
          if (!ProgressCounter) % 100 == 0:
              print str(!ProgressCounter) + " games processed."


    file.close()

    print "A total of " + str(!ProgressCounter) + " have been processed."

使用Netbeans IDE我收到以下错误:

  !RootDir     = "C:\\Users\\***\\Documents\\GoGames"
  ^
SyntaxError: mismatched input '' expecting EOF

我之前已经能够遍历代码到file.close(),在那里我发出错误“与外部缩进级别不匹配”。

任何人都能够正确地使用此代码的语法吗?

1 个答案:

答案 0 :(得分:2)

删除变量名,列表声明(!![])和except子句(except !ValueError)前面的感叹号,这不是有效的Python语法。