开发者

python: append a list of values into a list

c1=[]
      for row in c:
        c1.开发者_开发问答append(row[0:13])

c is a variable containing a csv file

i am going through every row in it and i want only the first 14 elements to be in the c1

what am i doing wrong?


Nicer:

c1= [row[:13] for row in c.readlines()]

if that doesn't work, you may not assigning to c properly.

Also keep in mind that if you want first 14 characters, you actually want to do row[:14] Then you get characters 0->13 inclusively, or 14 total.


That will not include the element indexed at [13].

c1=[]
  for row in c:
    c1.append(row[:14])

If you want the individual elements (the above code will append a list, much like a 2D array) you should append it in the following way:

    c1 += row[:14]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜