From 90731dab5dbffcdac00a3de948c692ace0e82cff Mon Sep 17 00:00:00 2001 From: kuzh Date: Wed, 17 Jan 2024 20:05:36 +0100 Subject: [PATCH] Create and save results as xlsx --- .vscode/launch.json | 2 - output.csv | 2 - speedtest.py | 5 +- utilities.py | 120 ++++++++++++++++++-------------------------- 4 files changed, 52 insertions(+), 77 deletions(-) delete mode 100644 output.csv diff --git a/.vscode/launch.json b/.vscode/launch.json index a15689b..96bdd41 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,8 +11,6 @@ "program": "${workspaceFolder}/speedtest.py", "console": "integratedTerminal", "args": [ - "--csv", - "--json" ], // "linux": { // "MIMode": "gdb", diff --git a/output.csv b/output.csv deleted file mode 100644 index ea7b55c..0000000 --- a/output.csv +++ /dev/null @@ -1,2 +0,0 @@ -download,upload,ping,server_url,server_lat,server_lon,server_name,server_country,server_cc,server_sponsor,server_id,server_host,server_d,server_latency,timestamp,bytes_sent,bytes_received,share,client_ip,client_lat,client_lon,client_isp,client_isprating,client_rating,client_ispdlavg,client_ispulavg,client_loggedin,client_country -0,0,6.93,http://ookla-osl.net.avur.no:8080/speedtest/upload.php,59.9494,10.7564,Oslo,Norway,NO,AVUR AS,18967,ookla-osl.net.avur.no:8080,5.7463776547060155,6.93,2024-01-01T21:26:53.963606Z,0,0,,84.215.59.36,59.955,10.859,Telia Norge AS,3.7,0,0,0,0,NO diff --git a/speedtest.py b/speedtest.py index ff7207a..7d8dc07 100755 --- a/speedtest.py +++ b/speedtest.py @@ -1945,7 +1945,8 @@ def shell(): results = speedtest.results # print(dict(str(results))) # os.makedirs('tmp', exist_ok=True) - utilities.write_to_csv(str(results)) + # utilities.convert_and_save_to_xlsx(str(results), "tmp/output.xlsx") + # utilities.convert_and_save_to_csv(str(results), "tmp/data.csv") printer('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: ' '%(latency)s ms' % results.server, quiet) @@ -1999,6 +2000,8 @@ def shell(): if args.share and not machine_format: printer('Share results: %s' % results.share()) + utilities.convert_and_save_to_xlsx(str(results), "tmp/output.xlsx") + def main(): try: diff --git a/utilities.py b/utilities.py index 37ca76b..5def226 100644 --- a/utilities.py +++ b/utilities.py @@ -1,76 +1,6 @@ -import logging import os -import shutil -import time -import csv -import json - - -def remove_directory(directory_path, n=100): - directory_path = str(directory_path) - if os.path.islink(directory_path): - os.unlink(directory_path) - elif os.path.exists(directory_path): - deleted = False - for i in range(0, n - 1): - try: - shutil.rmtree(directory_path, onerror=on_error) - deleted = True - break - - except OSError as err: - logging.error( - "Error while deleting directory {}, on attempt number {}: {}. Retrying...".format( - directory_path, i, err.strerror - ) - ) - time.sleep(1) - - if not deleted: - shutil.rmtree(directory_path, onerror=on_error) - - -def on_error(func, path, exc_info): - """ - Error handler for ``shutil.rmtree``. - - If the error is due to an access error (read only file) - it attempts to add write permission and then retries. - - If the error is for another reason it re-raises the error. - - Usage : ``shutil.rmtree(path, onerror=on_error)`` - """ - import stat - - if not os.access(path, os.W_OK): - # Is the error an access error ? - os.chmod(path, stat.S_IWUSR) - func(path) - else: - raise - - -def make_directory(directory_path): - directory_path = str(directory_path) - if not os.path.exists(directory_path): - os.makedirs(directory_path) - - -def make_clean_directory(directory_path): - directory_path = str(directory_path) - remove_directory(directory_path) - while os.path.exists(directory_path): - pass - make_directory(directory_path) - while not os.path.exists(directory_path): - pass - - -def delete_file(file): - file = str(file) - if os.path.exists(file): - os.remove(file) +import ast +import pandas as pd def write_to_csv(input_str): @@ -101,3 +31,49 @@ def write_to_csv(input_str): csv_writer = csv.writer(csvfile) csv_writer.writerow(header) csv_writer.writerow(values) + + +def convert_and_save_to_xlsx(data_str, file_name): + # Try to open the Excel file + try: + df = pd.read_excel(file_name) + except IOError: + # File doesn't exist, create a new one + df = pd.DataFrame() + + # Convert the input string to a dictionary + data_dict = ast.literal_eval(data_str) + + # Flatten the nested dictionaries + flattened_data = flatten_dict(data_dict) + + # Append the flattened data to the DataFrame + df = df._append(pd.DataFrame(flattened_data, index=[0]), ignore_index=True) + + # Save the DataFrame to an Excel file + df.to_excel(file_name, index=False) + +def flatten_dict(d, parent_key='', sep='_'): + """ + Flatten a nested dictionary by joining keys with a separator. + """ + items = [] + for k, v in d.items(): + new_key = f"{parent_key}{sep}{k}" if parent_key else k + if isinstance(v, dict): + items.extend(flatten_dict(v, new_key, sep=sep).items()) + else: + items.append((new_key, v)) + return dict(items) + +# Sample input string +sample_input = "{'download': 0, 'upload': 0, 'ping': 9.319, 'server': {'url': 'http://speedtest.nextgentel.no:8080/speedtest/upload.php', 'lat': '59.9494', 'lon': '10.7564', 'name': 'Oslo', 'country': 'Norway', 'cc': 'NO', 'sponsor': 'NextGenTel AS', 'id': '8018', 'host': 'speedtest.nextgentel.no:8080', 'd': 5.7463776547060155, 'latency': 9.319}, 'timestamp': '2024-01-01T20:06:56.839888Z', 'bytes_sent': 0, 'bytes_received': 0, 'share': None, 'client': {'ip': '84.215.59.36', 'lat': '59.955', 'lon': '10.859', 'isp': 'Telia Norge AS', 'isprating': '3.7', 'rating': '0', 'ispdlavg': '0', 'ispulavg': '0', 'loggedin': '0', 'country': 'NO'}}" + +# Specify the file name +file_name = "output.xlsx" + +# Call the function with the sample input and file name +convert_and_save_to_xlsx(sample_input, file_name) + + +# py .\speedtest.py --csv \ No newline at end of file