Last active
May 9, 2024 03:25
-
-
Save fernvenue/fdd3716af25589e30ed839f950531656 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: UTF-8 -*- | |
import re | |
import csv | |
def extract_clippings(clippings_file): | |
clippings = [] | |
with open(clippings_file, 'r', encoding='utf-8') as file: | |
data = file.read() | |
matches = re.findall(r'^(.*?)\s+\((.*?)\)\n- 您在第 (\d+) 页.*?添加于 (\d+.*?)\n\n(.*?)==========', data, re.DOTALL|re.MULTILINE) | |
for match in matches: | |
title, author, page, timestamp, clipping = match | |
clippings.append({ | |
'Title': title.strip(), | |
'Author': author.strip(), | |
'Page': int(page.strip()), | |
'Timestamp': timestamp.strip(), | |
'Clipping': clipping.strip() | |
}) | |
return clippings | |
def save_to_csv(clippings, csv_file): | |
with open(csv_file, 'w', newline='', encoding='utf-8') as csvfile: | |
fieldnames = ['Title', 'Author', 'Page', 'Timestamp', 'Clipping'] | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
for clipping in clippings: | |
clipping['Title'] = clipping['Title'].replace('', '') | |
writer.writerow(clipping) | |
if __name__ == "__main__": | |
clippings_file = "My Clippings.txt" | |
csv_file = "clippings.csv" | |
clippings = extract_clippings(clippings_file) | |
save_to_csv(clippings, csv_file) | |
print("Clippings extracted and saved:", csv_file) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: UTF-8 -*- | |
import csv | |
def convert_to_clippings(csv_file, output_file): | |
with open(csv_file, 'r', newline='', encoding='utf-8') as csvfile: | |
reader = csv.DictReader(csvfile) | |
clippings = [] | |
for row in reader: | |
title = row['Title'] | |
author = row['Author'] | |
page = row['Page'] | |
timestamp = row['Timestamp'] | |
clipping = row['Clipping'] | |
clippings.append(f"{title} ({author})\n- 您在第 {page} 页的标注 | 添加于 {timestamp}\n\n{clipping}\n==========\n") | |
with open(output_file, 'w', encoding='utf-8') as file: | |
file.writelines(clippings) | |
if __name__ == "__main__": | |
csv_file = "clippings.csv" | |
output_file = "My Clippings.txt" | |
convert_to_clippings(csv_file, output_file) | |
print("CSV file converted back to Kindle Clippings format:", output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment