File size: 1,223 Bytes
2706a1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv

def count_words_in_csv(input_file, output_file):
    # Set the CSV field size limit to a large value, avoiding the overflow issue
    csv.field_size_limit(10**7)  # 10 million, a high but safe limit
    
    # Open the input file with utf-8 encoding
    with open(input_file, 'r', encoding='utf-8') as infile:
        reader = csv.reader(infile)
        
        # Prepare the data for the output file
        output_data = [["Original Content", "Word Count"]]
        
        # Iterate through each row, count words, and add to output data
        for row in reader:
            content = row[0]  # Get the content from the single column
            word_count = len(content.split())  # Count the words
            output_data.append([content, word_count])
    
    # Write the output data to the output CSV file
    with open(output_file, 'w', newline='', encoding='utf-8') as outfile:
        writer = csv.writer(outfile)
        writer.writerows(output_data)
    
    print(f"Word count added to '{output_file}' successfully.")

# Usage example
count_words_in_csv(r"C:\Users\91745\OneDrive\Desktop\Github_analyser\output\local_repo\docs\quick_js_js.csv", "quickjsjs_count.csv")