开发者

Adding column In table with Python

Im trying to create a logging output using python 2.6.

The data come a database. What I would like to do is add a column to 开发者_StackOverflow中文版all rows with a time stamp = strftime("%Y-%m-%d %H:%M:%S"). There are about 50 rows.

Then drop into a csv table.

.append and .extend seem to add rows but not columns. Is there an easy way to do this?

Should I splice data to add col?


A quick example:

If you have a two-dimensional list like

l = [[1,2,3,4],
     [5,6,7,8],
     [9,10,11,12]]

then l.append(13) gets you

l = [[1,2,3,4],
     [5,6,7,8],
     [9,10,11,12],
     13]

which I assume is what you mean by "it adds rows, not columns".

You probably want l[0].append(13) which gives you

[[1,2,3,4,13],
 [5,6,7,8],
 [9,10,11,12]]

If you want to do this for all rows, you could use

for row in l:
    row.append(13)

giving you

[[1, 2, 3, 4, 13], 
 [5, 6, 7, 8, 13], 
 [9, 10, 11, 12, 13]]

Of course, in your case you will want to add the timestamp instead of 13, but the principle is the same. And then it's trivial to convert the 2D list into a csv object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜