python:将整行插入列表中
import csv
with open('thefile.csv', 'rb') as f:
data = list(csv.reader(f))
import collections
counter = collections.defaultdict(int)
for row in data:
counter[row[10]] += 1
with open('/pythonwork/thefile_subset11.csv', 'w') as outfile:
writer = csv.writer(outfile)
sample_cutoff=500
b[]
for row in data:
if counter[row[10]] >= sample_cutoff:
writer.writerow(row)
除了将数据写入文件之外,我还想将其插入到列表 b[] 中,
我可以执行 b.insert[row]
吗?
import csv
with open('thefile.csv', 'rb') as f:
data = list(csv.reader(f))
import collections
counter = collections.defaultdict(int)
for row in data:
counter[row[10]] += 1
with open('/pythonwork/thefile_subset11.csv', 'w') as outfile:
writer = csv.writer(outfile)
sample_cutoff=500
b[]
for row in data:
if counter[row[10]] >= sample_cutoff:
writer.writerow(row)
in addition to writing the data to a file, i would like to insert it into a list b[]
can i just do b.insert[row]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它是
b.append(row)
,但除此之外是的。而不是b[]
,而是b = []
。另一种方法是先创建列表,然后将列表的每个元素写入文件:It's
b.append(row)
, but otherwise yes. And instead ofb[]
you wantb = []
. Another way to do it would be to make the list first, and then just write each element of the list to the file:是的,
b = []
将支持insert
方法。语法如下。Yes,
b = []
would support aninsert
method. The syntax is as follows.它是 list.insert(index, item) 或 list.append(item)。
另外,
b[]
是名称错误和语法错误。我想你的意思是b = []
。It's list.insert(index, item) or list.append(item).
Also,
b[]
is a name error and syntax error. I suppose you meanb = []
.