zmrs163
級別: 家園?
![]() |
隨著一些機(jī)型轉(zhuǎn)換,我們的程序也需要進(jìn)行轉(zhuǎn)換,一些轉(zhuǎn)換可以通過軟件自動完成,一些轉(zhuǎn)換需要人工進(jìn)行更改。這個(gè)帖子主要討論的是地址變化時(shí)的轉(zhuǎn)換方式。 目前來說,三菱的梯形圖可以導(dǎo)出成CSV文件,所有的軟元件在這個(gè)CSV文件中都可以看到。如果我們直接操作這個(gè)CSV文件對程序進(jìn)行轉(zhuǎn)換,顯然可以更加方便得轉(zhuǎn)程序,因?yàn)槲覀兛梢越柚恍┠_本提升操作效率,一些情況下,地址存在重疊的情況,比如X0改成X10,但是原來的X10要改成X20,這種情況用軟件批量替換要分兩次走,一個(gè)不注意要進(jìn)行大量返工,以下是我自己寫的python腳本的代碼,自己隨意寫的,有能力有需要的同志可以自己制作類似的,含有FB的我目前還沒測試,不過應(yīng)該問題不大,畢竟只是實(shí)參需要替換一下。 import csv path = r"" #導(dǎo)出程序CSV文件的地址 path2 = r"" #生成一個(gè)文件,包含了所有的軟元件地址,需要手動在其中第二列填寫變更后的地址 path3 = r"" #生成的新文件的地址 before_list = [] #原程序所有的地址列表 address_dict = dict() #存儲映射關(guān)系的字典 # 讀取所有的地址 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,默認(rèn)好像就是這個(gè)編碼 s = input("任意鍵繼續(xù)讀取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] #替換主要由這三行進(jìn)行 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() |
---|---|
|