Python语法错误,命令,*args=行。拆分()

2024-09-30 01:31:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到一个command, *args = line.split()的Python语法错误

这是我的bce.py文件

import sys
import os

import pcn
import urp

class BCE(object):
    def __init__(self, inPath, outPath):
        self.inPath = inPath
        self.outPath = outPath
        self.eqs = {}
        self.operations = {
            "r": self.read_pcn,
            "!": self.do_not,
            "+": self.do_or,
            "&": self.do_and,
            "p": self.write_pcn,
            "q": self.quit
        }

        self.done = False

    def process(self, commandFilePath):
        with open(commandFilePath, "r") as f:
            for line in f:
               command, *args = line.split()
               self.operations[command](*args)

               if self.done:
                   return

    def read_pcn(self, fNum):
       _, self.eqs[fNum] = pcn.parse(os.path.join(self.inPath, fNum + ".pcn"))

    def write_pcn(self, fNum):
        with open(os.path.join(self.outPath, fNum + ".pcn"), "w") as f:
            pcn.write(f, None, self.eqs[fNum])

    def do_not(self, resultNum, inNum):
        self.eqs[resultNum] = urp.complement(self.eqs[inNum])

    def do_or(self, resultNum, leftNum, rightNum):
        self.eqs[resultNum] = urp.cubes_or(self.eqs[leftNum], self.eqs[rightNum])

    def do_and(self, resultNum, leftNum, rightNum):
        self.eqs[resultNum] = urp.cubes_and(self.eqs[leftNum], self.eqs[rightNum])

    def quit(self):
        self.done = True


Usage = """\
USAGE: {} COMMAND_FILE
"""

if __name__ == "__main__":
    if len(sys.argv) > 1:
        solutionDir = "BCESolutions"
        thisSolDir = os.path.join(solutionDir, sys.argv[1][-5])
        try:
            os.mkdir(thisSolDir)
        except OSError:
            # It's okay if it's already there
            pass

        bce = BCE("BooleanCalculatorEngine", thisSolDir)
        bce.process(sys.argv[1])
    else:
        print(Usage.format(sys.argv[0]))

这是我的pcn.py文件

from itertools import islice
from itertools import chain

def parse(filePath):
    with open(filePath, "rb") as f:
        # First line is size of array
        try:
            lines = iter(f)
            numVars = int(next(lines))
            cubeCount = int(next(lines))
            cubes = [None]*cubeCount

            for i in range(cubeCount):
                line = next(lines)
                cubes[i] = tuple(islice(map(int, line.split()), 1, None))

            return (numVars, tuple(cubes))

        except Exception as error:
            raise AssertionError("Bad pcn file {}".format(filePath)) from error

def write(f, numVars, cubes):
    endl = "\n"

    f.write(str(max(max(map(abs, cube)) for cube in cubes)))
    f.write(endl)
    f.write(str(len(cubes)))
    f.write(endl)

    cubes = tuple(set(tuple(sorted(cube, key=abs)) for cube in cubes))

    for cube in cubes:
        f.write(' '.join(map(str, chain((len(cube),), cube))))
        f.write(endl)

    f.write(endl)

Tags: importselfforosdefsyslinedo
1条回答
网友
1楼 · 发布于 2024-09-30 01:31:46

带有*star_target项的元组赋值只能在python3中使用。不能在python2中使用它。见PEP 3132 - Extended Iterable Unpacking。你知道吗

作为解决方法,只需一个目标,然后使用切片:

split_result = line.split()
command, args = split_result[0], split_result[1:]

相关问题 更多 >

    热门问题