使用python替换csv中的值 [英] Replacing values in csv by using python

查看:589
本文介绍了使用python替换csv中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def update(all_marks,stud_num,mark,column,result):
lines = [l for all in all_marks]

all_marks:
如果行中的stud_num:
lines [rows] [column] = mark

从这里,我想使用 lines [rows] [column] = mark 替换值。

它应该用mark替换现有的值。
但是定义行有一个问题。
任何人都知道如何解决?
感谢。



编辑:
下面是来自all_marks的数据示例:


[['a','','','','',''],



['b' ,'','','',''],



['d','','','','',''],



'['f','','','','''' ,'',''],



['g','','','','','']] $ b

我想在这里做的是用标记替换''中的值。



例如,def update(all_marks,'a','10',2,True):将返回


[['a', '','10','','',''],



['b','','','','' '],



['c','','','','',''],



['d','','','','',''],

['e','','' ,'','',''],



['f','','','','',''] >

['g','','','','','']]



b $ b

感谢您帮助新手。

解决方案

这里是您的函数的修改版本,将如预期地返回输出:

  def update(all_marks,stud_num,mark,column):
for i in range(len(all_marks)):
如果stud_num在all_marks [i]:
all_marks [i] [column] = mark
return all_marks

以下是它的工作原理:

 >> 
[['a','','','','',''],['b','','','','' c','','','','',''],['d','','','','' ,'','',''],['f','','','','' ']]

>>>> update(marks,'a','10',2)
[['a','','10','','',''],['b',' '','','',''],['c','','','','' ''],['e','','','','',''],['f','','' ,'','','','','']]

c $ c>标记现已修改

 >> 
[['a','','10','','',''],['b','','','','' 'c','','','','',''],['d','','','','' ','','',''],['f','','','','' '']]

如果要更改,以便更新只返回修改的数据更改该函数以下列方式:

  def update(all_marks,stud_num,mark,column):
tmp = all_marks
for i in range(len(tmp)):
如果tmp [i]中的stud_num:
tmp [i] [column] = mark
return tmp


def update(all_marks, stud_num, mark, column, result):
    lines = [l for l in all_marks]

    for row in all_marks:
        if stud_num in row:
            lines[rows][column] = mark

From here, I am trying to replace the value by using lines[rows][column] = mark.

It is supposed to replace the existing value with mark. But there's a problem with defining rows. Anyone knows how to fix? Thanks.

Edit: Here's sample of data from all_marks:

[['a', '', '', '', '', ''],

['b', '', '', '', '', ''],

['c', '', '', '', '', ''],

['d', '', '', '', '', ''],

['e', '', '', '', '', ''],

['f', '', '', '', '', ''],

['g', '', '', '', '', '']]

What I want to do here is to replace the value in '' with mark.

For example, def update(all_marks, 'a', '10', 2, True): will return

[['a', '', '10', '', '', ''],

['b', '', '', '', '', ''],

['c', '', '', '', '', ''],

['d', '', '', '', '', ''],

['e', '', '', '', '', ''],

['f', '', '', '', '', ''],

['g', '', '', '', '', '']]

Thanks for helping a newbie.

解决方案

Here is a modified version of your function that will return the output as expected:

def update(all_marks, stud_num, mark, column):
    for i in range(len(all_marks)):
        if stud_num in all_marks[i]:
            all_marks[i][column] = mark
    return all_marks

And here is how it works:

>>> marks
[['a', '', '', '', '', ''], ['b', '', '', '', '', ''], ['c', '', '', '', '', ''], ['d', '', '', '', '', ''], ['e', '', '', '', '', ''], ['f', '', '', '', '', ''], ['g', '', '', '', '', '']]

>>> update(marks,'a','10',2)
[['a', '', '10', '', '', ''], ['b', '', '', '', '', ''], ['c', '', '', '', '', ''], ['d', '', '', '', '', ''], ['e', '', '', '', '', ''], ['f', '', '', '', '', ''], ['g', '', '', '', '', '']]

Note that marks is now modified

>>> marks
[['a', '', '10', '', '', ''], ['b', '', '', '', '', ''], ['c', '', '', '', '', ''], ['d', '', '', '', '', ''], ['e', '', '', '', '', ''], ['f', '', '', '', '', ''], ['g', '', '', '', '', '']]

If you want to change that so that update simply returns a copy of modified data change the function in the following way:

def update(all_marks, stud_num, mark, column):
    tmp = all_marks
    for i in range(len(tmp)):
        if stud_num in tmp[i]:
            tmp[i][column] = mark
    return tmp

这篇关于使用python替换csv中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆