0

I need to manipulate the first element(which is an integer from 1 - 1000) of a csv file while also performing other operations.

Right now i have this code which prints a new row on every third row. But i also want to manipulate the first element with a counter on every third row by replacing the first element with a counter.

How my csv file after it is manipulated looks today:

1, information, info
2, info, info 
, , ,
3, info, info

How i want it to look after it is manipulated

1, info, info
2, info, info
, , ,
4, info, info

My code:

with open("mycsvfile.csv", 'r') as infile:
    readstream = csv.reader(infile, delimiter=',')
    with open("output.csv", 'wt') as output:
        outwriter = csv.writer(output, delimiter=',')
        i = 0
        #I want to manipulate the first element of the .csv-file here

        next(readstream) #to skip first row
        for row in readstream:
             outwriter.writerow(row)
             i += 1
         if i % 2 == 0:
             outwriter.writerow([])
8
  • 3
    Would you like us to guess how your .csv looks like ? ^^ Commented Mar 14, 2016 at 19:39
  • @Dex' ter - I edited the post and explained some more. Didn't think it was necessary to show since i only wanted to manipulate the first element. Commented Mar 14, 2016 at 19:43
  • 1
    You said first, but your example modifies the second? Commented Mar 14, 2016 at 19:45
  • How can we reproduce your problem if you don't provide your .csv ? Maybe with my own example it works ^^ Instead of adding some more words, you could've just add a part of the csv Commented Mar 14, 2016 at 19:45
  • 1
    I'm struggling to understand the expected goal, but could you not just use row[0] = i just before outwriter.writerow(row)? I think you're working with a nested list so you can redefine items in that list by their index Commented Mar 14, 2016 at 19:52

2 Answers 2

4

You are getting a nested list from the file you are reading. The items in the list are mutable, so you can modify items in them on each loop by using the index of the item you want to change:

for row in readstream:
    row[0] = i
    outwriter.writerow(row)
    i += 1
    if i % 2 == 0:
        outwriter.writerow([])
        i += 1
Sign up to request clarification or add additional context in comments.

3 Comments

Put a i += 1 after writing the blank row too.
@tdelaney this is the part I struggled with most. From the comments on OP, this was sufficient to close the question and give intended behaviour, I assumed the counter needed incrementing but not quite sure
@tdelaney looking back at the intended output after I unpicked it, you're correct. Edit made.
0

Not sure if this qualifies as a full answer, but I'm not allowed to comment, so please move this to where appropriate: You want to modify the first value inside your row, so what about something like:

if not i%3:
    row = modify(row[:1]) + row[1:] # where modify is some function that returns a list with a single modified value, i.e. modify = lambda x: [x.strip()] or something
    outwriter.writerow(row)

2 Comments

How do i declare the modify-variable?
It's just a function that can take in the value you want modified and do whatever to it: modify = lambda x: [x.strip()]. You don't actually need to define a function for that - do you just need to keep the first few characters? Oh and you need to make sure that your modify returns a list so you can perform the + op

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.