Upload 50k.py with huggingface_hub
Browse files
50k.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from multiprocessing import Pool
|
| 4 |
+
import time
|
| 5 |
+
from tqdm import tqdm
|
| 6 |
+
|
| 7 |
+
def process_rows(args):
|
| 8 |
+
rows, output_directory = args
|
| 9 |
+
for index, row in rows.iterrows():
|
| 10 |
+
# Generate the output text file path
|
| 11 |
+
text_filename = f"row_{index}.txt"
|
| 12 |
+
text_file_path = os.path.join(output_directory, text_filename)
|
| 13 |
+
|
| 14 |
+
# Write the row to a text file
|
| 15 |
+
with open(text_file_path, 'w') as text_file:
|
| 16 |
+
text_file.write(','.join(row.astype(str)))
|
| 17 |
+
|
| 18 |
+
# Directory containing the CSV files
|
| 19 |
+
csv_directory = "extracted_csv_files"
|
| 20 |
+
|
| 21 |
+
# Number of text files to generate
|
| 22 |
+
target_count = 50000
|
| 23 |
+
|
| 24 |
+
# Get the list of CSV files in the directory
|
| 25 |
+
csv_files = [os.path.join(csv_directory, file) for file in os.listdir(csv_directory) if file.endswith(".csv")]
|
| 26 |
+
|
| 27 |
+
# Create a directory to store the extracted text files
|
| 28 |
+
output_directory = "extracted_text_files_50k"
|
| 29 |
+
os.makedirs(output_directory, exist_ok=True)
|
| 30 |
+
|
| 31 |
+
# Initialize variables
|
| 32 |
+
total_count = 0
|
| 33 |
+
file_index = 0
|
| 34 |
+
|
| 35 |
+
# Start the timer
|
| 36 |
+
start_time = time.time()
|
| 37 |
+
|
| 38 |
+
# Create a progress bar
|
| 39 |
+
progress_bar = tqdm(total=target_count, unit='files')
|
| 40 |
+
|
| 41 |
+
# Process CSV files until the target count is reached
|
| 42 |
+
while total_count < target_count and file_index < len(csv_files):
|
| 43 |
+
csv_file_path = csv_files[file_index]
|
| 44 |
+
|
| 45 |
+
# Read the CSV file using pandas
|
| 46 |
+
df = pd.read_csv(csv_file_path)
|
| 47 |
+
|
| 48 |
+
# Get the number of rows in the CSV file
|
| 49 |
+
num_rows = len(df)
|
| 50 |
+
|
| 51 |
+
# Calculate the number of rows to extract from the current CSV file
|
| 52 |
+
rows_to_extract = min(target_count - total_count, num_rows)
|
| 53 |
+
|
| 54 |
+
# Extract the rows from the CSV file
|
| 55 |
+
rows = df.iloc[:rows_to_extract]
|
| 56 |
+
|
| 57 |
+
# Create a multiprocessing pool
|
| 58 |
+
pool = Pool()
|
| 59 |
+
|
| 60 |
+
# Process the rows in parallel
|
| 61 |
+
pool.map(process_rows, [(rows, output_directory)])
|
| 62 |
+
|
| 63 |
+
# Close the multiprocessing pool
|
| 64 |
+
pool.close()
|
| 65 |
+
pool.join()
|
| 66 |
+
|
| 67 |
+
total_count += rows_to_extract
|
| 68 |
+
file_index += 1
|
| 69 |
+
|
| 70 |
+
# Update the progress bar
|
| 71 |
+
progress_bar.update(rows_to_extract)
|
| 72 |
+
|
| 73 |
+
# Close the progress bar
|
| 74 |
+
progress_bar.close()
|
| 75 |
+
|
| 76 |
+
# End the timer
|
| 77 |
+
end_time = time.time()
|
| 78 |
+
|
| 79 |
+
# Calculate the execution time
|
| 80 |
+
execution_time = end_time - start_time
|
| 81 |
+
|
| 82 |
+
print(f"\nGenerated {total_count} text files.")
|
| 83 |
+
print(f"Execution time: {execution_time:.2f} seconds.")
|