Python: Append a list with a newline
<p>In the following code I am updating a list with two items by appending to it from within a for loop. I need a newline to be added after appending each string but I can't seem to be able to do it.</p>

<p>I thought it would have been:</p>

<pre><code>lines = []
for i in range(10):

    line = ser.readline()
    if line:
        lines.append(line + '\n')             #'\n' newline 
        lines.append(datetime.now())
</code></pre>

<p>But this only adds the '\n' as a string. I have also tried the same without without the quotes, but no luck.</p>

<p>I am getting this:</p>

<pre><code>['leftRaw; 928090;   0;  0.00;  0.00;  0.00;  0.00;  0.00;  0.00;      0.00;  0.00;\n', datetime.datetime(2016, 8, 25, 23, 48, 4, 517000), '\r\x00rightRaw; 928090;   0;  0.00;  0.00;  0.00;  0.00;  0.00;  0.00;  0.00;  0.00;\n', datetime.datetime(2016, 8, 25, 23, 48, 4, 519000), '\r\x00leftRaw; 928091;   0;  0.00;  0.00;  0.00;  0.00;  0.00;  0.00)]
</code></pre>

<p>But I want this:</p>

<pre><code>['leftRaw; 928090;   0;  0.00;  0.00;  0.00;  0.00;  0.00;  0.00;  0.00;  0.00;\n', 
datetime.datetime(2016, 8, 25, 23, 48, 4, 517000), 
'\r\x00rightRaw; 928090;   0;  0.00;  0.00;  0.00;  0.00;  0.00;  0.00;  0.00;  0.00;\n', 
datetime.datetime(2016, 8, 25, 23, 48, 4, 519000)]
</code></pre>

<p>Any ideas?</p>

<p>You can add all items to the list, then use .join() function to add new line between each item in the list:</p>

<pre><code>for i in range(10):
    line = ser.readline()
    if line:
        lines.append(line)
        lines.append(datetime.now())
final_string = '\n'.join(lines)
</code></pre>

python