Spaces:
Sleeping
Sleeping
| def read_kvl_file(file): | |
| data = {} | |
| current_key = None | |
| for line in file: | |
| line = line.decode().strip() # Convert bytes to string and strip whitespace | |
| if not line: # Skip empty lines | |
| continue | |
| if '=' in line: | |
| key, value = line.split('=') | |
| key = key.strip() | |
| value = value.strip() | |
| if value: # Non-empty value, treat as vector data | |
| if key not in data: | |
| data[key] = [] | |
| try: | |
| data[key].append(float(value)) | |
| except ValueError: | |
| data[key].append(value) | |
| else: # Empty value, treat as matrix data | |
| current_key = key | |
| if current_key not in data: | |
| data[current_key] = [] # Initialize list to store matrices | |
| elif current_key: | |
| row_values = line.split() | |
| if row_values: | |
| data[current_key].append([float(value) for value in row_values]) | |
| return data | |
| # def write_data_to_file(file_path, data): | |
| # with open(file_path, 'w') as file: | |
| # for key, values in data.items(): | |
| # file.write(key + ':\n') | |
| # if isinstance(values[0], list): # Matrix data | |
| # for row in values: | |
| # file.write(' '.join(row) + '\n') | |
| # else: # Vector data | |
| # for value in values: | |
| # file.write(value + '\n') | |
| # # Example usage | |
| # input_file_path = 'd:\Git\KVL_visualize\Turbidites_out.kvl' | |
| # output_file_path = 'd:\Git\KVL_visualize\Test.txt' | |
| # kvl_data = read_kvl_file(input_file_path) | |
| # write_data_to_file(output_file_path, kvl_data) |