Do you want to do something like this?

import csv
with open('large.csv','w') as f1:
    writer=csv.writer(f1, delimiter='\t',lineterminator='\n',)
    for i in range(1000000):
        row = [i + j*0.2 for j in range(i+1)]
        writer.writerow(row)

or also with the row/column headers:

import csv
with open('large.csv','w') as f1:
    writer=csv.writer(f1, delimiter='\t',lineterminator='\n',)
    writer.writerow([''] + range(1000000))
    for i in range(1000000):
        row = [i] + [i + j*0.2 for j in range(i+1)]
        writer.writerow(row)

The latter returns the following file (I have replaced 1000000 with 10):

        0       1       2       3       4       5       6       7       8       9
0       0.0
1       1.0     1.2
2       2.0     2.2     2.4
3       3.0     3.2     3.4     3.6
4       4.0     4.2     4.4     4.6     4.8
5       5.0     5.2     5.4     5.6     5.8     6.0
6       6.0     6.2     6.4     6.6     6.8     7.0     7.2
7       7.0     7.2     7.4     7.6     7.8     8.0     8.2     8.4
8       8.0     8.2     8.4     8.6     8.8     9.0     9.2     9.4     9.6
9       9.0     9.2     9.4     9.6     9.8     10.0    10.2    10.4    10.6    10.8
Answer from eumiro on Stack Overflow
Discussions

string - Write output from for loop to a csv in python - Stack Overflow
I am opening a csv called Remarks_Drug.csv which contains product names and mapped filenames in consecutive columns. I am doing some operations on the product column to remove all string content a... More on stackoverflow.com
🌐 stackoverflow.com
February 27, 2019
python - Writing to csv with for loops - Stack Overflow
I am trying to write a new row for each item in list_people with the name and age of the person. If there are 3 people in the list, my CSV should have 3 rows and 2 columns as shown below: Joann 15 More on stackoverflow.com
🌐 stackoverflow.com
August 11, 2017
Writing lines to a csv file in a for loop in python - Stack Overflow
I have a large csv file with about 5000 rows in it. The first column contains identifying names for each row i.e. LHGZZ01 The first 9 rows have LHGZZ01 as a name the next 10 have something else a... More on stackoverflow.com
🌐 stackoverflow.com
csv writer in a loop - Python - Stack Overflow
I'm trying to use csv writer in Python to write my output data to a file. When I just use the print command, the data looks good. But when I use the writerow command (line 20), nothing goes into th... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › how do i read a csv file and using a for loop, write to a new column?
r/learnpython on Reddit: How do I read a CSV file and using a for loop, write to a new column?
September 16, 2021 -

I have been able to get up to this point of reading my csv and printing out the strings I want. I can't figure out how to get the 'DictWriter' section of my script to write to a new column called "description"

My goal is to loop through each column/row and write a description.

I can get print() to work but nothing I have tried will get the writer to work. I am at a total loss

**Note**

The print() functions shown in the code below are written for readability of this post

import csv
import pandas as pd

f_name = '/Users/ashtoncarroll/Documents/github_repositories/Ashkiebear/projects/csv_desc_builder/csv_files/files/Shopify Listing Descriptions appended.csv'

with open(f_name, 'r') as csv_file:
    csv_reader = csv.DictReader(csv_file, delimiter=',')

    csv_reader.__next__()
    for row in csv_reader:
        brand = row['brand']
        part_type = row['part_type']
        model = row['model'] 
        part_number = row['part number']
        quantity = row['quantity']
        
    print(f'''
              New {part_type}!
              Fits {brand} Models:
              {model} 
              {part_number}
              Includes:
              {quantity}x {part_type}')
              '''

    with open('writer_file.csv', 'w') as wf:
        fieldnames = ['brand', 'part_type', 'model', 'part number', 'quantity', 'description']
        csv_writer = csv.DictWriter(csv_file, filenames=fieldnames)
        
        for row['description'] in csv_reader:
            csv_writer.writerow(f'''
                                    New {part_type}!
                                    Fits {brand} Models:
                                    {model} 
                                    {part_number}
                                    Includes:
                                    {quantity}x {part_type}')
                                     '''
🌐
Vertabelo Academy
academy.vertabelo.com › course › python-csv › writing › writing › writing-to-csv-files-in-a-loop
Read and Write CSV in Python | Learn Python | Vertabelo Academy
Of course, we normally don't use writerow() for each line manually, especially when we have lots of data. Typically, we generate the data on the fly, or have the data pre-calculated and stored in a list of lists. In such cases, we can use a for loop: data_to_save = [ ['Author', 'Title', 'Pages'], ['John Smith', 'Keep holding on', '326'], ['Erica Coleman', 'The beauty is the beast', '274'] ] with open('books.csv', mode='w', newline='') as csv_file: csv_writer = csv.writer(csv_file) for row in data_to_save: csv_writer.writerow(row)
Top answer
1 of 2
3
import csv
import pandas as pd

with open('Remarks_Drug.csv', newline='', encoding ='utf-8') as myFile:
    reader = csv.reader(myFile)
    mydrug = []
    for row in reader:
        product = row[0].lower()
        #print('K---'+ product)
        filename = row[1]
        product_patterns = ', '.join([i.split("+")[0].strip() for i in product.split(",")])
        mydrug.append([product_patterns, filename])

#     print(mydrug)

    df = pd.DataFrame(mydrug, columns=['product_patterns', 'filename'])
    print(df)
    df.to_csv('drug_output100.csv', sep=',', index=False)

This utilizes pandas library. If you're to deal with large csv files using pandas will be handy and efficient in terms of performance and memory. This is just an alternative solution for the above.

2 of 2
2

I hope this is the right way for you, if is not, tell me and we check.

import csv

with open('Remarks_Drug.csv') as myFile:
    reader = csv.reader(myFile)
    products_list = list()
    filenames_list = list()

    for row in reader:
        products_list.append(row[0].lower().split("+")[0].strip())
        filenames_list.append(row[1])

    for index, product in enumerate(products_list):
        with open ('drug_output100.csv', 'a') as csvfile:
            fieldnames = ['product_patterns', 'filename']
            print(fieldnames)
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            print(writer)
            writer.writerow({'product_patterns':product, 'filename':filenames_list[index]})
  1. Open the Remarks_Drug.csv file and create two list where store the row value elaborated as you prefer.
  2. Iterate on the product list and enumerate it so you have an index to use on the filename list.
  3. Open the output file and append to it the result.

You can also use pandas to elaborate csv files, faster and in a smart way.

Here the pandas solution:

import pandas as pd

def select_real_product(string_to_elaborate):
    return string_to_elaborate.split('+')[0].strip()

df = pd.read_csv("Remarks_Drug.csv", delimiter=',', names=("product", "filename"))

df['product'] = df['product'].apply(select_real_product)

df.to_csv("drug_output100.csv", sep=',', na_rep='empty',index_label=False, index=False)
🌐
Python Morsels
pythonmorsels.com › csv-writing
Writing a CSV file - Python Morsels
February 14, 2023 - You could use a for loop along with the writerow method: >>> import csv >>> with open("cities.csv", mode="wt", newline="") as csv_file: ... writer = csv.writer(csv_file) ... for row in cities: ... writer.writerow(row) ...
🌐
w3resource
w3resource.com › python-exercises › csv › python-csv-exercise-9.php
Python: Create an object for writing and iterate over the rows to print the values - w3resource
... import csv import sys with open('temp.csv', 'wt') as f: writer = csv.writer(f) writer.writerow(('id1', 'id2', 'date')) for i in range(3): row = ( i + 1, chr(ord('a') + i), '01/{:02d}/2019'.format(i + 1),) writer.writerow(row) print(open...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 49146035 › csv-writer-in-a-loop-python › 49146260
csv writer in a loop - Python - Stack Overflow
import requests from BeautifulSoup import BeautifulSoup import csv symbols = {'AMZN', 'BAC', 'GOOG', 'RCL'} with open('symbols.csv', "w") as csv_file: writer = csv.writer(csv_file, delimiter=',') for s in symbols: try: url1 ='https://research.tdameritrade.com/grid/public/research/stocks/fundamentals?symbol=' full_url = url1 + s response = requests.get(full_url) html = response.content soup = BeautifulSoup(html) for hist_div in soup.find("div", {"data-module-name": "HistoricGrowthAndShareDetailModule"}): EPS = hist_div.find('label').text print (s + ' ' + EPS) #this works and prints out good looking data #writer.writerow([s,EPS])<<this doesn't print anything to file except Exception as e: continue
Top answer
1 of 4
2

Although for this task you don't need it, I would take advantage of standard library modules when you can, like csv. Try something like this,

import os
import csv

csvfile = open('outputFileName.csv', 'wb')
writer = csv.writer(csvfile)

for filename in os.listdir('/'): # or C:\\ if on Windows
    writer.writerow([filename, len(filename)])

csvfile.close()
2 of 4
0

I'd probably change this:

for filename in os.listdir (image_path):
    print filename
    print len(filename)

To something like

lines = list()

for filename in os.listdir(image_path):
    lines.append("%s, %d" % (filename, len(filename)))

My version creates a python list, then on each iteration of your for loop, appends an entry to it.

After you're done, you could print the lines with something like:

for line in lines:
    print(line)

Alternatively, you could initially create a list of tuples in the first loop, then format the output in the second loop. This approach might look like:

lines = list()

# Populate list
for filename in os.listdir(image_path):
    lines.append((filename, len(filename))

# Print list
for line in lines:
    print("%s, %d" % (line[0], line[1]))

# Or more simply
for line in lines:
    print("%s, %d" % line)

Lastly, you don't really need to explicitly store the filename length, you could just calculate it and display it on the fly. In fact, you don't even really need to create a list and use two loops.

Your code could be as simple as

import sys, os

image_path = "C:\\"
for filename in os.listdir(image_path):
    print("%s, %d" % (filename, len(filename))
🌐
CodeProject
codeproject.com › Questions › 1243136 › Python-write-a-result-for-loop-to-csv-in-multiple
Python: write a result for loop to csv in multiple row
May 7, 2018 - Disclaimer: References to any specific company, product or services on this Site are not controlled by GoDaddy.com LLC and do not constitute or imply its association with or endorsement of third party advertisers
🌐
Humboldt State University
gsp.humboldt.edu › olm › Courses › GSP_318 › 02_X_3_1_WritingCSVFiles.html
GIS Programming With Python - Writing to Text Files
TheFile=open("C:/Temp/test2.csv","w") Index=0 while (Index<10): # go through the code below for each value from 0 to 9 TheFile.write(format(Index)+"\n") # Convert the Index to a string and write a line to the file Index+=1 TheFile.close() We can also write "grids" of values by putting one "for" loop inside another "for" loop.
🌐
Brown University
cs.brown.edu › courses › csci0050 › 2019 › Lectures › while-and-csv.html
CSCI 0050 - While loops and .csv files
for loops: used when you have a known and fixed amount of data · while loops: used when the size of data can't be known up front · In the case of a .csv file, there is a predictable amount of data. Python can look and know that for a given .csv, there is a specific number of rows.
Top answer
1 of 2
2

Leaving the with block closes with file. Therefore, the new_file function just opens and immediately closes a file.

You could do somthing like the following:

import csv

rowCounter     = 0
fileCounter    = 0
List_A         = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
List_B         = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
List_C         = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

# create file handle
csvfile = open('file-' + str(fileCounter) + '.csv', 'w')

rowWriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)

for word1 in List_A:
    for word2 in List_B:
        for word3 in List_C:
            sentence = word1 + word2 + word3
            rowWriter.writerow ([sentence])

            rowCounter += 1

            if rowCounter == 100:
                # close current filehandle
                csvfile.close()
                fileCounter += 1
                # open new file
                csvfile =  open('file-' + str(fileCounter) + '.csv', 'w')
                rowWriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
                rowCounter = 0

# close file
csvfile.close()

or with defining a function:

import csv

rowCounter     = 0
fileCounter    = 0
List_A         = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
List_B         = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
List_C         = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

def new_writer( csvfile, counter ):
     if csvfile: 
         csvfile.close()

     # open new file
     csvfile =  open('file-' + str(counter) + '.csv', 'w')
     rowWriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)

     counter += 1

     return rowWriter,csvfile,counter

rowWriter, csvFile, fileCounter = new_writer( None, fileCounter )

for word1 in List_A:
    for word2 in List_B:
        for word3 in List_C:
            sentence = word1 + word2 + word3
            rowWriter.writerow ([sentence])

            rowCounter += 1

            if rowCounter == 100:
                # close current file and open a new one
                rowWriter, csvfile, counter =  new_writer( csvfile, fileCounter )         
                rowCounter = 0

# close file
csvFile.close()
2 of 2
0

Thanks @desiato!

I accepted your answer, but ended up using lines 23-29 of your code and ended up with this (it works great!):

import csv

rowCounter     = 0
fileCounter    = 0
List_A         = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
List_B         = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
List_C         = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

with open('file-' + str(fileCounter) + '.csv', 'w') as csvfile:
    rowWriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)

    for word1 in List_A:
        for word2 in List_B:
            for word3 in List_C:
                sentence = word1 + word2 + word3
                rowWriter.writerow ([sentence])

                rowCounter += 1

                if rowCounter == 100:
                    csvfile.close()
                    fileCounter += 1
                    csvfile =  open('file-' + str(fileCounter) + '.csv', 'w')
                    rowWriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
                    rowCounter = 0
                else:
                    continue
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
How to Write to CSV File in a Loop? [SOLVED] - Raspberry Pi Forums
Image number " + str(no + 1) + " of " + str(number_of_images) + ".\n") file_size_list = [] with open(cfg["image_file_paths"]["save_path"] + goes_sat_dir + "/" + goes_dir + "/" + goes_dir + "_file_info_" + start_prog.strftime("%Y-%m-%d") + ".csv", "r", newline="") as csv_index: reader = csv.reader(csv_index) for row in reader: file_size_list.append(int(row[1])) min_size = file_prefixes(min(file_size_list)) max_size = file_prefixes(max(file_size_list)) avg_size = file_prefixes(sum(file_size_list)/len(file_size_list)) total_size = file_prefixes(sum(file_size_list)) if not speed_list: min_speed =