Python Forum
Add a new line to a CSV in one column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add a new line to a CSV in one column
#1
Hi,

I have a report that is stored in a list. One of the columns will return multiple entries. I would like to a new line between each entry. Is this possible?

My report code is

    with open(full_report_path, 'w') as f:
        writer = csv.writer(f)
        writer.writerow(field_names)
    # Sets the column order
    with open(full_report_path, 'a+', encoding='utf-8', newline='') as output_file:
        dict_writer = csv.DictWriter(output_file, order)
        dict_writer.writerows(computer_list)
At the moment it looks like this

[Image: before3.png]

I would like it to look like this

[Image: after2.png]

Thanks for the help
Reply
#2
Are you asking for each field to be on a separate line?
Is that correct, and if so, why?
Reply
#3
I think he's asking how do you insert a string into an array<String> at a certain location, and move all subsequent strings in that array to a point one index higher.

edit

Hard to see his current and desired outputs, he should type it out and put it in code tags.

double edit

myArray.insert(index, String)
Reply
#4
Thanks for replying. Sorry if I didn't explain it very well.

The cell with the manual clean up in it is made up of a list. At the moment it outputs as one long list making the column very wide. I would like the list, in that cell to output one below the other in the same cell

At the moment it is like this

alert1, alert2, alert3, alert4

and I would like it like this

alert1
alert2
alert3
alert4

This report have over 20 columns, but I only want this cell to output like the above. I hope that makes more sense.
Reply
#5
there are number of issues with your request
you write the list as one long string, not a list with multiple elements
I think you write a txt/csv file, but show view/formatting that is excel file/spreadsheet (i.e. you just view the csv file in excel). The plain text file has no concept of cell or new lines. Because it's just a long string you cannot easy manipulate it manually in excel
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
I had a feeling that would be the case. I am happy for it to look like this in Excel if this is possible

name, date, machine, alert1
blank cell, blank cell, blank cell, alert2
blank cell, blank cell, blank cell, alert3
name2, date2, machine2, alert
Reply
#7
We don't see your data, but instead using writerows() to write everything at once, iterate over elements of computer list, then for each element iterate over elements in the alerts list and write a row for each alert in the list, with rest elements blank, or repeat same values
import csv

data = [['row1_field1', 'row1_field2', ['row1_alert1', 'row1_alert2']],
        ['row2_field1', 'row2_field2', ['row2_alert1', 'row2_alert2', 'row2_alert3']]]

with open('report.csv', 'w', newline='') as f:
    csv_wrtr = csv.writer(f)
    csv_wrtr.writerow(['field1', 'field2', 'alert'])
    for row in data:
        field1, field2, alerts = row
        for alert in alerts:
            csv_wrtr.writerow([field1, field2, alert])
Output:
field1,field2,alert row1_field1,row1_field2,row1_alert1 row1_field1,row1_field2,row1_alert2 row2_field1,row2_field2,row2_alert1 row2_field1,row2_field2,row2_alert2 row2_field1,row2_field2,row2_alert3
if you replace last 2 lines with

        for idx, alert in enumerate(alerts):
            if idx:
                csv_wrtr.writerow(['', '', alert])
            else:
                csv_wrtr.writerow([field1, field2, alert])
you will get
Output:
field1,field2,alert row1_field1,row1_field2,row1_alert1 ,,row1_alert2 row2_field1,row2_field2,row2_alert1 ,,row2_alert2 ,,row2_alert3
using csv.DictWriter may be better
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
Thanks for the help. I will take a look and give it a try
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to do next line output from CSV column? atomxkai 2 2,087 Oct-02-2021, 01:00 AM
Last Post: Pedroski55
  [Solved] Reading every nth line into a column from txt file Laplace12 7 5,236 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  beginner text formatting single line to column jafrost 4 3,217 Apr-28-2021, 07:03 PM
Last Post: jafrost
  [gpxpy] "Error parsing XML: not well-formed (invalid token): line 1, column 1" Winfried 5 6,687 Jan-26-2020, 01:09 AM
Last Post: Winfried
  Writing values at a desired column in a line of text file Gupta 3 3,471 Jul-28-2017, 11:08 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020