0

对给定 python 代码的不同解释的可能原因是什么?

我有一个代码,我可以在计算机上毫无错误地执行,但在另一台计算机上输出错误。python 版本相同(2.7.12)。脚本的编码是相同的。我想知道什么可以解释这一点,因为这是我看到的不同代码解释的唯一两个原因。

这是代码的样子,使用 luigi (这里只是代码的一部分):

class ExampleClass(luigi.postgres.CopyToTable):

    def rows(self):
        """
        Return/yield tuples or lists corresponding to each row to be inserted.
        """
        with self.input()['Data'].open('r') as fobj:
            for line in fobj:
                yield line.strip('\n').split('\t')

当我在确实有错误的计算机上运行整个代码时(这是由我上面写的行引起的),我得到了这个:

IndentationError: unexpected indent

确实,代码中混合了空格和制表符。这很容易解决,这里没有问题,但我的问题是:什么可以解释解释上的差异?

我问是因为在通过用制表符替换空格解决了这个问题之后,我得到了其他不应该出现并且更难解决的错误,问题是代码应该是正确的,因为它可以在另一台计算机上运行,​​所以我不应该必须解决这些错误。

为了提供更多详细信息,这是我在解决缩进问题后遇到的第二个错误(我无法解决这个问题):

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: malformed \N character escape

它是由以下代码部分引起的:

class AnotherClass(ExampleClass):

    def copy(self, cursor, file):
        if isinstance(self.columns[0], six.string_types):
            column_names = self.columns
        elif len(self.columns[0]) == 2:
            column_names = [c[0] for c in self.columns]
        else:
            raise Exception('columns must consist of column strings or (column string, type string) tuples (was %r ...)' % (self.columns[0],))
        cursor.copy_expert('copy ' + self.table + ' from stdin with header csv delimiter \'' + self.column_separator + '\' null \'''\\\N''\' quote \'''\"''\' ', file)
4

1 回答 1

0

正如错误所说

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: malformed \N character escape

有问题的行是这一行:

cursor.copy_expert('copy ' + self.table + ' from stdin with header csv delimiter \'' + self.column_separator + '\' null \'''\\N''\' quote \'''\"' '\' ', 文件)

你的“N”应该是小写的,否则它不算作换行符。尝试这个:

cursor.copy_expert('copy ' + self.table + ' from stdin with header csv delimiter \'' + self.column_separator + '\' null \'''\\\n''\' quote \'''\"''\' ', file)
于 2017-06-21T12:38:12.067 回答