0

編集:私は単純なエラーを作成しました'1,2,3 +\nのテスト'は'1,2,3のテスト'+'\n'または'1,2,3のテスト\n'

ファイルから条件付きでテキストを読み取ろうとしています。my_file.readline()をいくつかと比較したときにTrueを返す条件を理解できません

my_file.readline()=='文字列リテラル'は常にfalseを返します。

with open('text.txt','w') as my_file:
    my_file.write('testing 1,2,3'+'\n'+'filter me'+'\n')  #writing arbitrary text to my  file


with open('text.txt','r') as my_file:
    str_from_file = my_file.readline()        # first line should be 'testing 1,2,3'
    print str_from_file == 'testing 1,2,3+\n' #both print False
    print str_from_file == 'testing 1,2,3'    #what string literal would print true?
    print str_from_file

明らかに、私はPythonとコーディングの巨大なナブです。Pythonで5日目です。

4

1 に答える 1

3

文字列を比較しています:

'testing 1,2,3'+'\n'  # 'testing 1,2,3\n'

'testing 1,2,3+\n'

2番目の文字列に追加の'+'文字が貼り付けられていることに注目してください。

于 2013-03-20T01:03:17.757 に答える