我在製表符分隔格式的文件尾隨換行符,例如,Python的 - 文件內容嵌套列表
123 abc
456 def
789 ghi
我希望寫功能將文件的內容轉換成一個嵌套列表。到目前爲止我曾嘗試:
def ls_platform_ann():
keyword = []
for line in open("file", "r").readlines():
for value in line.split():
keyword.append(value)
和
def nested_list_input():
nested_list = []
for line in open("file", "r").readlines():
for entry in line.strip().split():
nested_list.append(entry)
print nested_list
。
前者創建一個嵌套列表,但包含\ n和\ t字符。後者不會生成嵌套列表,而是會生成許多沒有\ n和\ t字符的等效列表。
任何幫助?
問候, 小號;-)
當迭代文本文件時,不需要'.readlines()'。另外你的'ls_platform_ann' **不會**創建嵌套列表。或者你沒有向我們展示所有相關的代碼 – SilentGhost 2010-03-09 16:32:58