Jan-19-2024, 10:36 AM
In Python, you can convert a .txt file to a .csv file using the csv module. Here's a simple example assuming your .txt file is structured with lines of values separated by spaces or tabs:
import csv # Specify the path for the input .txt file input_txt_path = 'input.txt' # Specify the path for the output .csv file output_csv_path = 'output.csv' # Read data from the .txt file with open(input_txt_path, 'r') as txt_file: # Assuming the values in each line are separated by spaces or tabs lines = [line.strip().split() for line in txt_file] # Write data to the .csv file with open(output_csv_path, 'w', newline='') as csv_file: csv_writer = csv.writer(csv_file) csv_writer.writerows(lines) print(f'Conversion from {input_txt_path} to {output_csv_path} completed.')