Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suggestions for passing large table between Python and C#

Tags:

python

c#

file

I have a C# application that needs to be run several thousand times. Currently it precomputes a large table of constant values at the start of the run for reference. As these values will be the same from run to run I would like to compute them independently in a simple python script and then just have the C# app import the file at the start of each run.

The table consists of a sorted 2D array (500-3000+ rows/columns) of simple (int x, double y) tuples. I am looking for recommendations concerning the best/simplest way to store and then import this data. For example, I could store the data in a text file like this "(x1,y1)|(x2,y2)|(x3,y3)|...|(xn,yn)" This seems like a very ugly solution to a problem that seems to lend itself to a specific data structure or library I am currently unaware of. Any suggestions would be welcome.

like image 789
Mandelbrot Avatar asked Jan 21 '23 16:01

Mandelbrot


2 Answers

I would go for a simplified csv file. Given that all your values are numbers, you can read them in C# using

File.ReadAllText(filename).Split(',')

You can find more C# options for csv here

On Python you can use the csv module to read and write them. Better explanation here, but the short of it is

import csv
writer = csv.writer(filename)
writer.writerows(data)

Using CSV also gives you flexibility for future improvements, as well as exporting and importing from other programs like Excel for further processing.

like image 143
Muhammad Alkarouri Avatar answered Jan 29 '23 09:01

Muhammad Alkarouri


You may consider running IronPython - then you can pass values back and forth across C#/Python

like image 29
Wayne Werner Avatar answered Jan 29 '23 09:01

Wayne Werner