\n
または改行文字をPythonのdict値から削除するにはどうすればよいですか?Dicts Python 3からの改行文字の削除
testDict = {'salutations': 'hello', 'farewell': 'goodbye\n'}
testDict.strip('\n') # I know this part is incorrect :)
print(testDict)
\n
または改行文字をPythonのdict値から削除するにはどうすればよいですか?Dicts Python 3からの改行文字の削除
testDict = {'salutations': 'hello', 'farewell': 'goodbye\n'}
testDict.strip('\n') # I know this part is incorrect :)
print(testDict)
ちょうどそれを反復し、値にstr.rstrip()
を適用し、その場で辞書を更新するには:新しい辞書を作成するには
for key, value in testDict.items():
testDict[key] = value.rstrip()
、あなた辞書の理解を使用することができます:
testDict = {key: value.rstrip() for key, value in testDict.items()}
ありがとう!それはトリックでした!助けてくれてありがとう! –
辞書理解用途:
testDict = {key: value.strip('\n') for key, value in testDict.items()}
ディクショナリオブジェクトから改行を削除しようとしています。 すべてのディクショナリキーを繰り返し、その値を更新します。
for key in testDict.keys():
testDict[key] = testDict[key].strip()
これはトリックです。
なぜあなたはそれを*辞書*に入れる前に取り除かなかったのですか?それはキーではない値です。 – jonrsharpe
http://stackoverflow.com/questions/8907788/iterating-over-a-dictionary-in-python-and-stripping-white-space –