2016-05-26 1 views
-2

現在、プログラマが2つのファイルをリコールするコードを実行しています。つまり、次のコードはそれらを結合しますが、ファイルからインポートするコードを変更します。 ValueError:基数10のint()のリテラルが無効です。python help!無効なリテラルfor int()for base 10:

ここに私のコードもあります。次のエラーが発生しました:compressed_sentence [(int(i)-1)変数は、プログラムによってdeteminedされている場合、それは仕事が

uncompressed = 0 

file1 = open ("NonDupT2.txt" , "r") 
compressed_sentence=file1.read() 
file1.close() 

file1 = open ("PositionT2.txt" , "r") 
compressed_Positionsonly=file1.read() 
file1.close() 

compressed_Positions= compressed_Positionsonly.split() 

print(str(compressed_Positions)) 

for i in compressed_Positions: 
    if compressed_sentence[(int(i)-1)]==uncompressed:  
     print(compressed_sentence[(int(i))]) 
     uncompressed = compressed_sentence[(int(i))] 

    else: 
     print(compressed_sentence[(int(i)-1)]) 
     uncompressed=compressed_sentence[(int(i)-1)] 

    print(str(int(i))) 

uncompressed = 0 

compressed_sentence = ['hello' , 'hello' , 'why' , 'hello' , 'lmao'] 
compressed_Positions = ['1' , '1' , '2' , '1' , '3'] 

print(str(compressed_Positions)) 

for i in compressed_Positions: 

    if compressed_sentence[(int(i)-1)]==uncompressed:  
     print(compressed_sentence[(int(i))]) 
     uncompressed = compressed_sentence[(int(i))] 

    else: 
     print(compressed_sentence[(int(i)-1)]) 
     uncompressed=compressed_sentence[(int(i)-1)] 

    print(str(int(i))) 

答えて

0

compressed_Positionsが誤って読み取られるという問題があります。ファイルは内容を1つの文字列として読み込みます。その文字列は、テキストファイルの内容全体です。

split演算子は、文字列のリストを作成します。そのリストの最初の要素は、ファイルから最初のスペースまでのすべてであり、それは

'[1,' 

ように見えるそして、それがint(によって解釈されるものである)、それは動作しません。私はあなたのPositionT2.txtが含まれていることを考える:それは含まれていた場合

[1, 1, 2, 1, 3] 

それは動作します:

1 1 2 1 3 

は、プログラムが明示的な定義と例と同じように動作させるために、あなたはのためのスプリット演算子が必要compressed_sentencesだけでなく、あなたNonDupT2.txtファイルが

hello hello why hello lmao 

の代わりにスペースが含まれている必要があり、また、あなたのテキストファイル内のセパレータとして改行を使用することができます。

幸運、 ジュスト

関連する問題