作者lthstar (Never Say Never)
看板Python
标题[问题] 请问读取CSV档,如何让他LIST方式呈现
时间Fri Aug 26 15:54:28 2011
正在学习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
1F:推 swpoker:strip() 08/26 16:06
2F:→ lthstar:谢谢!我刚试了,\n消掉了,但John Cleese名字重复了@@ 08/26 17:06