从行中获取值并删除前导0并添加到字符串

时间:2019-06-14 14:30:25

标签: python csv

我正在尝试将.dat转换为.csv。我需要帮助的是从当前行中删除前导零,并使数字成为变量,然后将其添加到字符串中。这是.dat文件:

0004|IP
0006|IP
0008|IP

它当前正在写入文件:

('0004\\Registers,Register  21',  ',IP',  'Store    -  Register  21',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0004\\Registers,Register  22',  ',IP',  'Store    -  Register  22',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0004\\Registers,Register  23',  ',IP',  'Store    -  Register  23',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0004\\Registers,Register  24',  ',IP',  'Store    -  Register  24',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0004\\Registers,Register  25',  ',IP',  'Store    -  Register  25',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0004\\Registers,Register  26',  ',IP',  'Store    -  Register  26',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0006\\Registers,Register  21',  ',IP',  'Store    -  Register  21',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0006\\Registers,Register  22',  ',IP',  'Store    -  Register  22',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0006\\Registers,Register  23',  ',IP',  'Store    -  Register  23',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0006\\Registers,Register  24',  ',IP',  'Store    -  Register  24',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0006\\Registers,Register  25',  ',IP',  'Store    -  Register  25',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0006\\Registers,Register  26',  ',IP',  'Store    -  Register  26',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0008\\Registers,Register  21',  ',IP',  'Store    -  Register  21',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0008\\Registers,Register  22',  ',IP',  'Store    -  Register  22',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0008\\Registers,Register  23',  ',IP',  'Store    -  Register  23',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0008\\Registers,Register  24',  ',IP',  'Store    -  Register  24',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0008\\Registers,Register  25',  ',IP',  'Store    -  Register  25',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

('0008\\Registers,Register  26',  ',IP',  'Store    -  Register  26',  '',  'LAN  (TCP)',  '',  '',  '',  '',  '',  '',  '',  '',  '0x1')

我需要在'store'后面加上不带零的数字,这是另一件事,您还可以帮助我从输出中删除括号和引号吗?谢谢!

import os
import csv

exists = os.path.isfile('stores.dat')
if exists:
    print('creating phone book')
    with open('stores.dat', 'r') as input_file:
        lines = input_file.readlines()
        newLines = []
        for line in lines:
            registers2 = 21
            while registers2 < 27:
                newLine = line[:4] + '\Registers,' + 'Register ' + str(registers2), ',IP','Store ' + '' + ' - Register ' + str(registers2), '', "LAN (TCP)", '', '', '', '','','', '', '', '0x1'
                newLines.append([newLine])
                registers2 = registers2 + 1
    with open('file.csv', 'w') as output_file:
        file_writer = csv.writer(output_file)
        file_writer.writerows(newLines)

这就是我需要的样子:

0004\Registers,Register 21,IP,Store 4 - Register 21,,LAN (TCP),,,,,,,,,0x1
0004\Registers,Register 22,IP,Store 4 - Register 22,,LAN (TCP),,,,,,,,,0x1
0004\Registers,Register 23,IP,Store 4 - Register 23,,LAN (TCP),,,,,,,,,0x1
0004\Registers,Register 24,IP,Store 4 - Register 24,,LAN (TCP),,,,,,,,,0x1
0004\Registers,Register 25,IP,Store 4 - Register 25,,LAN (TCP),,,,,,,,,0x1
0004\Registers,Register 26,IP,Store 4 - Register 26,,LAN (TCP),,,,,,,,,0x1

1 个答案:

答案 0 :(得分:0)

csv.writer.writerows列出列表。不要尝试自己在行中添加逗号。这就是csv模块的用途。

而是一次只读取一行文件,然后分别写入每一行。您可以使用csv模块进行读写:

import os
import csv

exists = os.path.isfile('stores.dat')
if exists:
    print('creating phone book')

    # When using csv, open files with newline=''
    with open('stores.dat','r',newline='') as input_file:
        with open('file.csv','w',newline='') as output_file:
            reader = csv.reader(input_file,delimiter='|')
            writer = csv.writer(output_file)
            for line in reader:
                for registers2 in range(21,27):
                    # Python 3.6+ f-strings for easier formatting.
                    # Make a list of columns.
                    newLine = [f'{line[0]}\Registers',f'Register {registers2}',line[1],f'Store {int(line[0])} - Register {registers2}','','LAN (TCP)','','','','','','','','','0x01']
                    writer.writerow(newLine)

输出:

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 22,,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