为每个商店在第6行或第8行之后添加2行

时间:2019-06-20 13:39:45

标签: python csv

我正在将.dat转换为csv。一切正常,但是我需要在添加6个寄存器之后或在添加8个寄存器之后添加CC和DD控制器。我尝试过,但是没有成功。这是.dat:

0004 | IP

0006 | IP

0008 | IP

除了没有空行

这是我的代码:

        logging.debug("option 1 selected")
        # check if file exists
        exists = os.path.isfile('stores.dat')

        if exists:
            print('creating phone book')
            with open('stores.dat') as csvinput, open('file.csv', 'w') as csvoutput:
                csvreader = csv.reader(csvinput, delimiter="|")
                csvwriter = csv.writer(csvoutput)
                for input_line in csvreader:
                    if input_line[0] == "0008":
                        registers = range(21, 29)
                    else:
                        registers = range(21, 27)
                    for register in registers:
                        output_line = ["{}\Registers".format(input_line[0]),
                                       "Register {}".format(register),
                                       input_line[1],  # IP
                                       "Store {} - Register {}".format(int(input_line[0]), register),
                                       None,
                                       "LAN (TCP)",
                                       None, None, None, None, None, None, None, None,
                                       "0x01"]

                        csvwriter.writerow(output_line)



  while input_line[0] != "0008" and register == 27:
          CCcontroller = input_line[0] + "\Controllers", 'CC 
Controller', "IPcc", 'Store ' + input_line[0].lstrip(
                            '0') + ' - CC Controller', '', "LAN (TCP)", 
     '', '', '', '', '', '', '', '', '0x1'

           DDcontroller = input_line[0]+ "\Controllers", 'DD 
Controller', "IPdd", 'Store ' + input_line[0].lstrip(
                            '0') + ' - DD Controller', '', "LAN (TCP)", 
  '', '', '', '', '', '', '', '', '0x1'

                        csvwriter.writerow(CCcontroller)
                        csvwriter.writerow(DDcontroller)

这是当前输出:

 0004\Registers,Register 21,IP,Store 4 - Register 21,,LAN (TCP),,,,,,,,,0x01
 0004\Registers,Register 22,IP,Store 4 - Register 22,,LAN (TCP),,,,,,,,,0x01
 0004\Registers,Register 23,IP,Store 4 - Register 23,,LAN (TCP),,,,,,,,,0x01
 0004\Registers,Register 24,IP,Store 4 - Register 24,,LAN (TCP),,,,,,,,,0x01
 0004\Registers,Register 25,IP,Store 4 - Register 25,,LAN (TCP),,,,,,,,,0x01
 0004\Registers,Register 26,IP,Store 4 - Register 26,,LAN (TCP),,,,,,,,,0x01
 0006\Registers,Register 21,IP,Store 6 - Register 21,,LAN (TCP),,,,,,,,,0x01
 0006\Registers,Register 22,IP,Store 6 - Register 22,,LAN (TCP),,,,,,,,,0x01
 0006\Registers,Register 23,IP,Store 6 - Register 23,,LAN (TCP),,,,,,,,,0x01
 0006\Registers,Register 24,IP,Store 6 - Register 24,,LAN (TCP),,,,,,,,,0x01
 0006\Registers,Register 25,IP,Store 6 - Register 25,,LAN (TCP),,,,,,,,,0x01
 0006\Registers,Register 26,IP,Store 6 - Register 26,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 21,IP,Store 8 - Register 21,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 22,IP,Store 8 - Register 23,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 23,IP,Store 8 - Register 23,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 24,IP,Store 8 - Register 24,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 25,IP,Store 8 - Register 25,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 26,IP,Store 8 - Register 26,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 27,IP,Store 8 - Register 26,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 28,IP,Store 8 - Register 26,,LAN (TCP),,,,,,,,,0x01

所需的输出

 0004\Registers,Register 21,IP,Store 4 - Register 21,,LAN (TCP),,,,,,,,,0x01
 0004\Registers,Register 22,IP,Store 4 - Register 22,,LAN (TCP),,,,,,,,,0x01
 0004\Registers,Register 23,IP,Store 4 - Register 23,,LAN (TCP),,,,,,,,,0x01
 0004\Registers,Register 24,IP,Store 4 - Register 24,,LAN (TCP),,,,,,,,,0x01
 0004\Registers,Register 25,IP,Store 4 - Register 25,,LAN (TCP),,,,,,,,,0x01
 0004\Registers,Register 26,IP,Store 4 - Register 26,,LAN (TCP),,,,,,,,,0x01
 0004\Controllers,CC Controller,IP,Store 6 - CC Controller,,LAN (TCP),,,,,,,,,0x1
 0004\Controllers,DD Controller,IP,Store 6 - DD Controller,,LAN (TCP),,,,,,,,,0x1
 0006\Registers,Register 21,IP,Store 6 - Register 21,,LAN (TCP),,,,,,,,,0x01
 0006\Registers,Register 22,IP,Store 6 - Register 22,,LAN (TCP),,,,,,,,,0x01
 0006\Registers,Register 23,IP,Store 6 - Register 23,,LAN (TCP),,,,,,,,,0x01
 0006\Registers,Register 24,IP,Store 6 - Register 24,,LAN (TCP),,,,,,,,,0x01
 0006\Registers,Register 25,IP,Store 6 - Register 25,,LAN (TCP),,,,,,,,,0x01
 0006\Registers,Register 26,IP,Store 6 - Register 26,,LAN (TCP),,,,,,,,,0x01
 0006\Controllers,CC Controller,IP,Store 6 - CC Controller,,LAN (TCP),,,,,,,,,0x1
 0006\Controllers,DD Controller,IP,Store 6 - DD Controller,,LAN (TCP),,,,,,,,,0x1
 0008\Registers,Register 21,IP,Store 8 - Register 21,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 22,IP,Store 8 - Register 23,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 23,IP,Store 8 - Register 23,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 24,IP,Store 8 - Register 24,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 25,IP,Store 8 - Register 25,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 26,IP,Store 8 - Register 26,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 27,IP,Store 8 - Register 26,,LAN (TCP),,,,,,,,,0x01
 0008\Registers,Register 28,IP,Store 8 - Register 26,,LAN (TCP),,,,,,,,,0x01
 0008\Controllers,CC Controller,IP,Store 6 - CC Controller,,LAN (TCP),,,,,,,,,0x1
 0008\Controllers,DD Controller,IP,Store 6 - DD Controller,,LAN (TCP),,,,,,,,,0x1

0 个答案:

没有答案