随着一些机型转换,我们的程序也需要进行转换,一些转换可以通过软件自动完成,一些转换需要人工进行更改。这个帖子主要讨论的是地址变化时的转换方式。
目前来说,三菱的梯形图可以导出成CSV文件,所有的软元件在这个CSV文件中都可以看到。如果我们直接操作这个CSV文件对程序进行转换,显然可以更加方便得转程序,因为我们可以借助一些脚本提升操作效率,一些情况下,地址存在重叠的情况,比如X0改成X10,但是原来的X10要改成X20,这种情况用软件批量替换要分两次走,一个不注意要进行大量返工,以下是我自己写的python脚本的代码,自己随意写的,有能力有需要的同志可以自己制作类似的,含有FB的我目前还没测试,不过应该问题不大,毕竟只是实参需要替换一下。
import csv
path = r"" #导出程序CSV文件的地址
path2 = r"" #生成一个文件,包含了所有的软元件地址,需要手动在其中第二列填写变更后的地址
path3 = r"" #生成的新文件的地址
before_list = [] #原程序所有的地址列表
address_dict = dict() #存储映射关系的字典
# 读取所有的地址
r = 1
with open(path, mode='r',encoding='utf-16-le') as file:
for line in file:
line_splited = line.split('\t')
if r >= 4:
add = line_splited[3].strip('"')
if add != '' and add not in before_list:
before_list.append(add)
r += 1
# 生成CSV,存储所有的地址,需要手动在其中第二列填写变更后的地址
s = input("是否生成TRANS,Y/N")
if s=="Y":
with open(path2, mode='w',encoding='utf-16-le',newline="") as file:
csv_writer = csv.writer(file)
before_list.sort()
for add in before_list:
csv_writer.writerow([add])
# 自行填写TRANS后再读取,wps保存编码为ANSI,默认好像就是这个编码
s = input("任意键继续读取TRANS")
with open(path2, mode='r') as file:
csv_reader = csv.reader(file)
for row in csv_reader:
if row[1] != "":
address_dict[row[0]] = row[1]
for k,v in address_dict.items():
print(k,v)
# 重新生成程序文件
index = 1
with open(path, mode='r',encoding='utf-16-le') as file:
new_file = open(path3,mode='w',encoding='utf-16-le')
for line in file:
if index >= 4:
r = [s.strip("\"") for s in line.split('\t')]
old_address = r[3] #替换主要由这三行进行
new_address = address_dict.get(r[3],r[3])
new_file.write(line.replace(old_address,new_address))
else:
new_file.write(line)
index += 1
new_file.close()