[問題] 請問讀取CSV檔,如何讓他LIST方式呈現

看板Python作者 (Never Say Never)時間14年前 (2011/08/26 15:54), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串1/2 (看更多)
正在學習python中,目前正在寫讀取csv檔, 裡面內容如下: John Cleese, Ministry of Silly Walks, 5555421, 2011/10/27 Eric Idle, Spamalot, 55553311, 2011/3/29 圖片: http://img696.imageshack.us/img696/1309/2011826053846.jpg
我的程式: def load_friends(filename): """takes the name of a file containing friends information as described in the introduction and returns a dictionary containing information about the friends in the file load_friends(str) -> dict(str:str) """ f = open(filename, 'U') info = {} for i in f: i_info =i.split(',') info[i_info[0]]=i_info[1:] f.close() print"your current friend list:" return info 但最後return 回來都會出現多出\n 如下: your current friend list: {'John Cleese': ['Ministry of Silly Walks', '5555421', '27-Oct\n'], 'Eric Idle': ['Spamalot', '55553311', '29-Mar\n']} 想請問要怎麼把\n拿掉呢? 假如我想要一段資料分行列的話, 要怎麼寫呢? 問題可能很簡單,但我一直試,就拿不掉\n, 用with 寫的話,是不會有\n出來,不過會多一個[ ]出來 我for迴圈底下是這樣設的 for i in loader: info.setdefault(i[0],[]).append(i[1:]) 可以幫我看一下嗎? 謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 124.189.208.13

08/26 16:06, , 1F
strip()
08/26 16:06, 1F

08/26 17:06, , 2F
謝謝!我剛試了,\n消掉了,但John Cleese名字重複了@@
08/26 17:06, 2F
文章代碼(AID): #1ELr4ttT (Python)
文章代碼(AID): #1ELr4ttT (Python)