1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
import time
def txt2json(s, name): with open(s + name + ".txt", 'r', encoding='UTF-8') as file_object: lines = file_object.readlines() with open(s + name + "_json.txt", 'w', encoding='UTF-8') as file: file.write('[\n') for line in lines: file.write(line.rstrip() + ',\n')
with open(s + name + "_json.txt", 'rb+') as file: file.seek(-3, 2) file.truncate()
with open(s + name + "_json.txt", 'a', encoding='UTF-8') as file: file.write('\n]')
path = "C:/Users/Lin/Desktop/" lists = ["publish_1950_mag_papers_10"] for i in lists: start = time.perf_counter() txt2json(path, i) end = time.perf_counter() print(i, end - start)
|