我有50個值,看起來像這樣的列表:我可以使用Python格式化列表中的文本嗎?
['xxxxxx\n', 'xxxxxx\n', 'xxxxxx\n', 'xxxxxx\n', 'xxxxxx \n', 'xxxxxx\n',
' xxxxxx \n', 'xxxxxx \n', 'xxxxxx\n', ...]
我想打印的清單作爲一個名單,但格式化列表中的文本。我想刪除該單詞前後的空格(在此示例中用xxxxxx替換)以及使用.title()
函數。
我已經試過這樣做,但我得到的錯誤:
AttributeError: 'list' object has no attribute 'strip'
我的理解是錯誤的,但我不知道是否有任何其他的方式來格式化文本列表內。
延伸閱讀:http://docs.python.org/tutorial/ datastructures.html#列表理解 – cheeken 2012-02-21 00:43:17
這是完美的!非常感謝:) – emagdnim 2012-02-21 00:47:22