0

Why doesn't the 3rd row of the CSV output have "hola"?

The (if levels[a] == "3") should ensure it's set to "hola".

Thanks for any ideas or help!

#-*- coding: utf-8 -*-
import csv
levels = [["1"], ["2"], ["3"]]
def column1Logic(self):
    self.column1 = "logic worked"
    self.column1 = self.greeting
class Row(object):
    column1 = "name"
    greeting = "oh"
    def __init__(self, level, greeting):
        self.level = level
        self.greeting = greeting
    def rowEntry(self, level, greeting):
        column1Logic(self)
        lol = [[self.column1]]
        lol[0] = self.column1
        file_writer.writerow([o for o in lol])
with open("test.csv", "wb") as test_file:
    file_writer = csv.writer(test_file)
    for a in range(0, len(levels)):
        if levels[a] == "3":
            greeting = "hola"
            food = Row(levels[a], greeting)
            food.rowEntry(levels[a], greeting)
        else:
            greeting = "hi"
            food = Row(levels[a], greeting)
            food.rowEntry(levels[a], greeting)

1 Answer 1

1

You are iterating over a list of lists. Therefore you should check if you get ['3'] instead of just '3'.

# -*- coding: utf-8 -*-
import csv
levels = [["1"], ["2"], ["3"]]
def column1Logic(self):
    self.column1 = "logic worked"
    self.column1 = self.greeting

class Row(object):
    column1 = "name"
    greeting = "oh"
    def __init__(self, level, greeting):
        self.level = level
        self.greeting = greeting
    def rowEntry(self, level, greeting):
        column1Logic(self)
        lol = [[self.column1]]
        lol[0] = self.column1
        file_writer.writerow([o for o in lol])

with open("test.csv", "wb") as test_file:
    file_writer = csv.writer(test_file)
    for a in range(0, len(levels)):
        print levels[a]
        if levels[a] == ['3']:  # instead of levels[a] == "3"
            greeting = "hola"
            food = Row(levels[a], greeting)
            food.rowEntry(levels[a], greeting)
        else:
            greeting = "hi"
            food = Row(levels[a], greeting)
            food.rowEntry(levels[a], greeting)
Sign up to request clarification or add additional context in comments.

2 Comments

OH MY GOSH, THANK YOU SO MUCH!!!! to @ Khalil Ammour he commented it there in the fix (# instead of levels[a] == "3")
@Bob Willaker yep, that is how you accept the answer

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.