-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
37 lines (25 loc) · 1.05 KB
/
utilities.py
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
31
32
33
34
35
36
37
'''
This script provides utility functions for working with data and text processing.
'''
import base64
import pandas as pd
def to_csv_download_link(df, filename):
"""
Generates a link to download a Pandas DataFrame as a CSV file.
Input: df: Pandas DataFrame to be downloaded.
filename: Name of the CSV file
Output:
A string representing an HTML link. Clicking the link will trigger the download of the DataFrame as a CSV file.
"""
csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode()
return f'<a href="data:file/csv;base64,{b64}" download="{filename}.csv">Download {filename}.csv</a>'
def remove_incomplete_sentence(text):
''' Removes incomplete sentences from a text.
Input: text: Input text containing sentences.
Output: A string with incomplete sentences removed.
'''
sentences = text.split(". ")
if not text.endswith('.'):
sentences = sentences[:-1]
return '. '.join(sentences)