Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 960607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:12:31+00:00 2026-05-16T01:12:31+00:00

i am reading a csv file into a data: def get_file(start_file): #opens original file,

  • 0

i am reading a csv file into a data:

def get_file(start_file): #opens original file, reads it to array
  with open(start_file,'rb') as f:
    data=list(csv.reader(f))

i need to go through every row and add a value to row[1] like this. initially row[1] = 'Peanut', i need to add ‘Butter’ so the result would be

row[1]='PeanutButter'

i need to do this for every row like this

for row in data:
  row+='Butter'

is this the correct way of doing it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:12:32+00:00Added an answer on May 16, 2026 at 1:12 am

    You want

    for row in data:
        row[ 1 ] += "Butter"
    

    but the right way to do this is not to iterate through every row of data again but to modify the way you generate data in the first place. Go look at my answer in your other question.


    Copy-paste from your previous question

    def get_file( start_file )
        f = csv.reader( start_file )
            def data( csvfile ):
                for line in csvfile:
                    line[ 1 ] += "butter"
                    yield line
        return data( f )
    

    which you use like

    lines = get_file( "my_file.csv" )
    for line in lines:
        # do stuff
    

    Explanation

    The issue here is that we want to modify the data held in data. We could look through and change every element in data, but that’s slow, especially given that we’re going to look through every element again shortly. Instead, a much nicer way is to change the lines as they are inserted into the data holder, because that saves you one pass.

    Here goes:

    f = csv.reader( start_file )

    I have modified the code to use csv.reader, because that’s a much more robust way of reading CSV data. It’s basically a trivial change; it works like open but each line is a tuple of the values, already separated for you.

    def data( csvfile )

    This is different! Instead of making data a variable, we’re making it a function. That doesn’t sound right, but bear with me.

    for line in csvfile:

    OK, data is a function of csvfile so we’re just iterating through the lines in the first argument. So far, so good.

    line[ 1 ] += butter

    This is the change you wanted to make: we add “butter” to the second element of line. You could also make other changes here: add a column to each row, delete columns, skip rows, the list goes on!

    yield line

    This is clever Python trickery. It basically works like return, but without stopping the function from running. Instead, it is paused until we ask for the next value.

    So now data is a function which returns each of the lines (modified just as you wanted them) in turn. What now? Easy:

    return data( f )

    Make get_file return data!

    What does this mean? Well, we can now use it as the iterable in a for loop:

    for line in get_file( "my_file.csv" ):

    and everything will work!!! Magic!! =p

    A couple of other points:

    • Previously, you used with ... as ... syntax. This is a context manager: it automatically closes the file when you’re done with it. But in this case we don’t want to do that, since we are reading lines from the file as we go through. Casting it to a list copies the whole thing into memory, which is sloooooow. You should add f.close() elsewhere in the code if you’re worried about memory leaks.

    • I had another point, but I can’t remember it…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a csv file into a variable data like this: def get_file(start_file):
i am reading a csv file into a list of a list in python.
I'm reading in a csv file of time-series data into a C++ program. My
I have been writing an implementation for reading a .csv data file into C#
Im reading data from a speadsheet and moving into a notepad csv file, in
I am loading a csv file via numpy.loadtxt into a numpy array. My data
I am able to get the CSV file data into array, but when it
I'm reading into a csv file an extracting a piece of data with the
I'm adding data from a csv file into a database. If I open the
I have a System.Data.DataTable which is populated by reading a CSV file which sets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.