我正在将.dat转换为csv。一切正常,但是我需要为0008添加8个寄存器,其余的保持为6。这是.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', 'r') as input_file:
#open stores.dat
lines = input_file.readlines()
newLines = [] # new line array
for line in lines:
registers2 = 21 #for the while loop
while registers2 <= 30: #while loop for selection 1, suppposed to print 6 registers for each store
if registers2 < 27:
stores = line[:4] #truncates the stores.dat after 4 chars
registers = stores + "\Registers" , 'Register ' + str(registers2),IP,'Store ' + stores.strip('0') + '' + ' - Register ' + str(registers2), '', "LAN (TCP)", '', '', '', '','','', '', '', '0x1'
newLines.append(registers)
registers2 = registers2 + 1
with open('file.csv', 'w',newline='') as output_file:
file_writer = csv.writer(output_file)
file_writer.writerows(newLines)
print('phonebook saved as file.csv')
else:
print('stores.dat does not exist')
IP出于安全性而更改。如您所见,while循环运行了8次,但它为每个存储添加了6个寄存器。如果您可以通过使它能够在将来添加具有8个寄存器的存储的方式来帮助我(那么这不是一次性的)。谢谢
这是当前输出:
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
期望的输出
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
最佳答案
with open('stores.dat') as csvinput, open('file.csv', 'w') as csvoutput:
csvreader = csv.reader(csvinput, delimiter="|") # Your input can be read with csv module
csvwriter = csv.writer(csvoutput)
# For each line of stores.dat
for input_line in csvreader:
if input_line[0] == "0008":
# Create 8 registers if first field is 0008
registers = range(21, 29)
else:
# Create 6 registers
registers = range(21, 27)
# Write one line per register per input line
for register in registers:
output_line = ["{}\Registers".format(input_line[0]),
"Register {}".format(register),
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)
file.csv的内容:
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
0008\Registers,Register 27,IP,Store 8 - Register 27,,LAN (TCP),,,,,,,,,0x01
0008\Registers,Register 28,IP,Store 8 - Register 28,,LAN (TCP),,,,,,,,,0x01
关于python - 为8008添加8个寄存器,其余为6个寄存器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56650465/