0

次のような数字とコメントの行を持つファイルがあるとします。

#comments
12 #this is number
2.4 #this is float

ファイルを読み取り、数字をリストに追加します。数字だけを取得しようとしていますが、どういうわけか #this is number と #this is float を追加します。

4

3 に答える 3

4

使用できますsplit

>>> 'foo #comment'.split('#', 1)[0]
'foo '
>>> 'foo comment'.split('#', 1)[0]
'foo comment'
于 2013-04-29T02:16:50.033 に答える