看板 Python 關於我們 聯絡資訊
正在學習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
swpoker:strip() 08/26 16:06
lthstar:謝謝!我剛試了,\n消掉了,但John Cleese名字重複了@@ 08/26 17:06