Python - 如何更新文件中的特定列?

时间:2015-04-24 14:29:02

标签: python

我在SOF(stackoverflow)中找到了其他方法,但它对我的查询/问题工作来说太复杂了...... 看这个例子:

DATA.TXT

  

结构:[代码] \ t [名称] \ t [定量。 PROD。买了\ t [价值花了]   [折扣] \ n“

1    Jennifer O. Ingram    10 265.65 151
2    Lacy N. Fields    120 2645.65 15
3    Blythe P. Abbott    105 2665.65 150
4    Alyssa Y. Cobb    170 2685.65 152
5    Peter Z. May    1450 265.655 154
6    MacKenzie A. Santos    10 265.65 15
7    Kevyn C. Willis   150 2665.65 154
8    Nissim E. Ward    1450 26675.65 158
9    Duncan J. Armstrong    10 2685.65 156
10    Jesse B. Barnett    1120 2635.65 150

使用客户代码,购买的产品数量以及使用折扣计算购买的价值进行买入。所以......

应用程序将请求用户代码执行此操作...当应用获取代码时,他将使用此工作代码搜索用户:

userCode = raw_input ("Informe o código do usuário: ")
database = open("database","r+")
dbLines = database.readlines()
database.seek(0)
userNotFound = True
for line in dbLines:
    if line.startswith(userCode+'\t'):
        print "\nUsuário Encontrado."
        print line
        userNotFound = False
if (userNotFound == True):
    print "Usuário não encontrado...\n"
x = raw_input("Pressione 'ENTER' para continuar ou digite 's' para sair...")
if (x == 's'):
    database.close()
    return
os.system('clear')
database.close()
subMenu_3()

但看看问题......我知道如何搜索,但我不知道如何更新。

1 个答案:

答案 0 :(得分:0)

看看这是否适合你

with open(file, 'a') as file:
    for line in file:
       list_col = line.split('\t')
       list_col[3] # This will be your **COLUM1** , do whatever you want with it