• 更改文件,某一行


     1 def fetch(data):
     2     print('这是查询功能')
     3     tag = False
     4     data = 'aaaa %s\n'%data
     5     res_data = []
     6     with open('bbb','r',encoding='gbk') as file:
     7         for i in file:
     8             if i == data:
     9                 tag = True
    10                 continue
    11             if tag == True and i.startswith('aaa'):
    12                 break
    13             if tag == True:
    14                 res_data.append(i)
    15                 print(i)
    16     return res_data
    17 def add():
    18     pass
    19 def delete():
    20     pass
    21 def change(data):
    22     print('你要更改的数据是%s'%data)
    23     old_addr =data[0]['backend']
    24     old_to = "        server  %s  98798  %s\n"%(data[0]['record']['server'],data[0]['record']['98798'])
    25     need_to = "        server  %s  98798  %s\n"%(data[1]['record']['server'],data[1]['record']['98798'])
    26     res = fetch(old_addr)
    27     print(res)
    28     if old_to in res:
    29         a = res.index(old_to)
    30         res[a] = need_to
    31         res.insert(0,'aaaa %s\n'%old_addr)
    32         tag = False
    33         write_or = True
    34         old_addr2 = 'aaaa %s'%old_addr
    35         with open('bbb','r') as old_file,\
    36                 open('bbb_new','w') as new_file:
    37             for backend in old_file:
    38                 if backend.strip() == old_addr2:
    39                     tag = True
    40                     continue
    41                 elif backend.startswith('aaaa'):
    42                     tag = False
    43                 if not tag:
    44                     new_file.write(backend)
    45                 if tag and write_or:
    46                     for i in res:
    47                         new_file.write(i)
    48                     write_or = False
    49 
    50     else:
    51         print('没有要修改的数据')
    52     return '更改成功'
    53 
    54 if __name__ =="__main__":
    55     msg = '''
    56     1:查询
    57     2;增加
    58     3:删除
    59     4:更改
    60     5:退出'''
    61     func_list = {
    62         '1':fetch,
    63         '2':add,
    64         '3':delete,
    65         '4':change
    66     }
    67     while True:
    68         print(msg)
    69         choice = input("请输入需要选择的功能:")
    70         if not choice: continue
    71         if choice =='5':break
    72         data = input('请输入数据:')
    73         if choice != '1':
    74             data = eval(data)
    75         ress = func_list[choice](data)
    76         print(ress)
    77 输出:
    78     1:查询
    79     2;增加
    80     3:删除
    81     4:更改
    82     5:退出
    83 请输入需要选择的功能:4
    84 请输入数据:[{'backend':'www.asia.com','record':{'server':'jdfjsk','98798':'sajdfkjahskjf'}},{'backend':'www.asia.com','record':{'server':'aaaaa','98798':'bbbbbb'}}]
    85 你要更改的数据是[{'backend': 'www.asia.com', 'record': {'server': 'jdfjsk', '98798': 'sajdfkjahskjf'}}, {'backend': 'www.asia.com', 'record': {'server': 'aaaaa', '98798': 'bbbbbb'}}]
    86 这是查询功能
    87         server  jdfjsk  98798  sajdfkjahskjf
    88 
    89         server  jdfjsdsk  98798  sajdfksasjahskjf
    90 
    91         server  jdfsdjsk  98798  sajdfksdjahskjf
    92 
    93         server  jdfxcjsk  98798  sajdfkjsaahskjf
    94 
    95         server  jdfjsxck  98798  sajdfkxxzjahskjf
    96 
    97 ['        server  jdfjsk  98798  sajdfkjahskjf\n', '        server  jdfjsdsk  98798  sajdfksasjahskjf\n', '        server  jdfsdjsk  98798  sajdfksdjahskjf\n', '        server  jdfxcjsk  98798  sajdfkjsaahskjf\n', '        server  jdfjsxck  98798  sajdfkxxzjahskjf\n']
    98 None
  • 相关阅读:
    第十次作业
    第八次作业
    作业七--1
    作业五
    作业六
    作业四
    作业一
    作业三
    作业2
    jsp第一次作业
  • 原文地址:https://www.cnblogs.com/ch2020/p/12383387.html
Copyright © 2020-2023  润新知