Write multiple values in a csv cell without brackets in python
<p>I have a list of objects. Each object have different attributes.
One of which is a list
I would like to export this objects to a csv file that looks like</p>

<pre><code>a, "1,2,3"
b,"1,2,3"
</code></pre>

<p>so that the csv is readable in a Excel program</p>

<p>My code looks something like this:</p>

<pre><code>final_list = []
for a in list:
        first_value = a.first_value
        list2 = []
        for b in a.list:
            list2.append(b)

        final_list.append(first_value)
        final_list.append(list2)
with open('file' ) as f:
        writer = csv.writer(f)
        writer.writerows(final_list)
    pass           
</code></pre>

<p>which results in this csv rows with the brackets and quotes that i don't want</p>

<pre><code>1, "['1','3','4']"
2,"['1','2','3']"
</code></pre>

<p>I need 2 values per row: <code>a</code> and <code>1,2,3</code> as string</p>

<p>if you do that:</p>

<pre><code>    final_list.append(first_value)
    final_list.append(list2)
</code></pre>

<p>you create a row with a value and a list.</p>

<p>The <code>csv</code> module performs a <code>str</code> conversion when writing, which explains you see the string as it is printed when you debug in python.</p>

<p>Instead do:</p>

<pre><code>    final_list.append(a.first_value)  # string
    final_list.append(",".join(a.list))  # composes a coma separated string with the contents of a.list
</code></pre>

<p>result:</p>

<pre><code>a,"1,2,3"
</code></pre>

<p>(quotes protecting the commas so it is seen as a single cell)</p>

python excel csv