【发布时间】:2011-11-13 06:41:48
【问题描述】:
由于某种原因,当我尝试将逗号分隔的项目拆分到一个新列表中然后将它们插入到更高的列表中时,我得到了这个回溯:
Traceback (most recent call last):
File "S:/Personal Folders/Andy/Python Projects/People Cancelled/Analyze Customers Test.py", line 15, in <module>
text[x] = textnew
TypeError: list indices must be integers, not str
这是我的代码:
from __future__ import division
from __future__ import print_function
in_file = open("s:/Personal Folders/Andy/Python Projects/People Cancelled/Analyze Authorize Truncated.csv")
text = in_file.readlines()
in_file.close()
header = text[0:1]
text = text[1:]
for x in text:
textnew = x.split(",")
text[x] = textnew
print(text)
正在使用的数据示例:
['3545869260,59.95,AUTH_CAPTURE,Jack,Franklin,810-555-2222,jack@francypants.com,01-Apr-2011 05:24:10 PM PDT\n', '354589999,0,VOID,Jacob,Rasnip,8224309464,goodness@finland.com,01-Apr-2011 05:24:10 PM PDT\n']
我基本上想将列表中的每条逗号分隔的信息变成一个单独的列表,所以基本上是一个主列表中的一堆列表。
谢谢!
【问题讨论】:
-
你能举一个简单的例子吗?
标签: python string list iteration