2017-06-21 19 views
0

給定python代碼的不同解釋的可能原因是什麼?對Python代碼有不同解釋的原因?

我有一個代碼,我可以在計算機上執行沒有錯誤,但在另一個上輸出錯誤。 python版本是相同的(2.7.12)。 腳本的編碼是相同的。 我想知道什麼可以解釋這一點,因爲這些是我看到不同代碼解釋的唯一兩個原因。

下面是代碼的樣子,用路易吉(這裏僅僅是代碼的一部分):

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) 
+2

你可以發佈在使用標籤替換所有空格後觀察到的錯誤嗎? –

+3

上述代碼的傳輸方法是什麼? –

+0

答案是你的錯誤信息:縮進。所以你可以自己修復它。 –

回答

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 \'''\"''\' ', file)

您的「N」應該小寫,否則它不會算作換行符。試試這個:

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

正如我在文章中解釋的,這不是解決這個錯誤,這是關於爲什麼此代碼在另一臺計算機上工作(因爲它工作),而不是在這臺計算機上。我只給了錯誤報告和代碼來幫助理解。 – Ashargin