Append to Previous .csv File Export

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

Append to Previous .csv File Export

Postby elizabeth.milks@lmco.com » Tue Jun 29, 2021 2:57 pm

I'm not sure if this is the right board to post to, if it's not I can post this question on another board here, but anyways:

I recently started using Cameo Systems Modeler and currently have a validation suite in use to track metrics via validation-based metric suites and represent the data with metric tables. I have no problems exporting the metric tables to .csv files, however I'm trying to keep a record of metrics over time. When I choose "write to file" the layout in the .csv (as well as if I use .xlsx) gets messed up and the headers are removed, making it difficult to use any of the data.

Is there any way to add to the .csv upon export without destorying the initial export's formatting? Maybe a script can be written rather than hitting "Export/Write to File"?

Thanks in advance!
elizabeth.milks@lmco.com
Forum Newbie
Forum Newbie
 
Posts: 1
Posts Rating:0
Joined: Tue Jun 29, 2021 2:25 pm

Re: Append to Previous .csv File Export

Postby confectionerylolli@gmail.com » Wed Jul 21, 2021 3:40 am

It would be nice if someone has any answer to this. I am interested too.
confectionerylolli@gmail.com
Forum Newbie
Forum Newbie
 
Posts: 1
Posts Rating:0
Joined: Wed Jul 21, 2021 3:40 am

Re: Append to Previous .csv File Export

Postby sempafa@gmail.com » Thu Jul 29, 2021 11:47 pm

In your case as you already have the data in the form of list of dictionaries I think you may wanna try using csv.Dictwriter()

A sample code piece:

Code: Select all
import csv
lstdc = [{'name':'Jack', 'age': 26},
        {'name':'John', 'age': 27},
        {'name':'Lisa', 'age': 36},
        {'name':'Adam', 'age': 16}]

fieldNames = list((lstdc[0]).keys())

with open('list_of_dict_to_csv.csv','w', newline='\n') as f:
    writer = csv.DictWriter(f, fieldNames)
    writer.writeheader()
    for val in lstdc:
        writer.writerow(val)

you can replace the lstdc with latest_submissions and list_of_dict_to_csv.csv with my_file.csv

Replacing the iteration of list dictionaries with built in writerows()

Code: Select all
with open('list_of_dict_to_csv.csv','w', newline='\n') as f:
    writer = csv.DictWriter(f, fieldNames)
    writer.writeheader()
    writer.writerows(lstdc)

Hope this helps!

lowes employee portal
sempafa@gmail.com
Forum Newbie
Forum Newbie
 
Posts: 1
Posts Rating:0
Joined: Thu Jul 29, 2021 11:43 pm


Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 1 guest

cron