user2877133 February 2016

Writing a row in Python, what is the best data structure to use?

I am trying to write headers for a .csv file (1 row)

My current way of doing this, I think is inefficient

    headers = "Color:," + color + ',' \
              "State:," + state + ',' \
              "Age," + str(age)

    writer.writerow((headers.split(','))

So the first row of every csvfile looks like

Color:, Red, State:, California, Age:, 22

Is there a better way in doing this, instead of a string...

Thanks!

Answers


Fishstick February 2016

You can skip the string concatenation and make a list instead of having to split on commas.

headers = ['Color:', color, 'State:', state, 'Age:', str(age)]
writer.writerow(headers)

This will accomplish what you have already but I am concerned about data in the header. What is this accomplishing?

Post Status

Asked in February 2016
Viewed 1,923 times
Voted 10
Answered 1 times

Search




Leave an answer