0

What are the possible reasons for a different interpretation of a given python code?

I have a code which I can execute with no errors on a computer, but which outputs errors on another one. The python versions are the same (2.7.12). The encoding of the scripts are the same. I wonder what could explain this because these are the only two reasons I see for a different code interpretation.

Here is what the code looks like, using luigi (here is only a part of the code) :

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')

When I run the whole code on the computer where i do have an error (which is caused by the lines I wrote above), I get this :

IndentationError: unexpected indent

And there is, indeed, a mix of spaces and tabs in the code. It is easy to solve, no problem here, but my question is about : What could explain that difference in the interpretation?

I'm asking because after solving this by replacing spaces with tabs, I got other errors that should not appear either and that are harder to solve, and the thing is the code is supposedly correct as it works on the other computer so I should not have to solve these errors.

To give more details, here is the second error I get after solving the indentation problems (I don't manage to solve this one) :

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

And it is caused by the following part of the code :

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)
Ashargin
  • 498
  • 4
  • 11

1 Answers1

0

As the error says

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

The line in question is this one:

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

Your "N" should be lowercase otherwise it doesn't count as a newline character. Try this:

cursor.copy_expert('copy ' + self.table + ' from stdin with header csv delimiter \'' + self.column_separator + '\' null \'''\\\n''\' quote \'''\"''\' ', file)
BoilingFire
  • 191
  • 1
  • 9
  • As I explained in the post, this is not about solving this error, this is about understanding why this code works on another computer (because it DOES work), and not on this computer. I only gave the errors reports and the codes to help understand. – Ashargin Jun 21 '17 at 13:02