Untitled
unknown
plain_text
a year ago
2.5 kB
9
Indexable
import os
# Đường dẫn đến thư mục chứa các file
folder_path = 'D:\.venv\inputcsv' # Thay đổi thành đường dẫn thư mục của bạn
substring = " - interval 4 hour"
# Lặp qua tất cả các file trong thư mục
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
# Kiểm tra xem có phải là file không (không phải thư mục)
if os.path.isfile(file_path):
# Đọc nội dung file
with open(file_path, 'r', encoding='utf-8') as file:
content = file.readlines() # Đọc từng dòng và lưu vào danh sách
new_content = []
# Xử lý nội dung nếu cần
for line in content:
"""
Tìm chỉ số đầu tiên của substring trong câu.
"""
index = line.find(substring)
if index != -1: # trong câu có substring
for i in range(index - 1, -1, -1):
if line[i] == ' ' or i == 0:
if i != 0:
attr = line[i+1: index]
else:
attr = line[i: index]
timestamp_replace = 'cast(FORMAT_TIMESTAMP("%Y-%m-$s %H:%M:%S", ' + attr + ", 'America/New_York') as timestamp)"
line = line.replace(substring, "", 1)
line = line.replace(attr, timestamp_replace, 1)
break
index = line.find(substring)
if index != -1: # trong câu có substring
for i in range(index - 1, -1, -1):
if line[i] == ' ' or i == 0:
if i != 0:
attr = line[i+1: index]
else:
attr = line[i: index]
timestamp_replace = 'cast(FORMAT_TIMESTAMP("%Y-%m-$s %H:%M:%S", ' + attr + ", 'America/New_York') as timestamp)"
line = line.replace(substring, "", 1)
line = line.replace(attr, timestamp_replace, 1)
break
new_content.append(line)
# Ghi lại nội dung đã thay đổi vào file
with open(file_path, 'w', encoding='utf-8') as file:
for item in new_content:
file.write(f"{item}")
print("Hoàn thành thay thế và lưu file.")
Editor is loading...
Leave a Comment