Suggestions for transferring a large table between Python and C #

I have a C # application that needs to run several thousand times. He is currently pre-calculating a large table of constant values ​​at the start of the run for reference. Since these values ​​will be the same from run to run, I would like to calculate them myself in a simple python script, and then just ask the C # application to import the file at the beginning 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 on the best / easiest way to store and import this data. For example, I could store 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 that I currently don't know about. Any suggestions are welcome.

+3
source share
6 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(',')

# csv

Python csv . ,

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

CSV , , Excel, .

+2

IronPython - #/Python

+2

NetCDF / HDF5. HDF5, , , .NET, PyTables Python.

+2

# "constants.bin" . , "constants.bin" . , , .

int[,] constants;

if(!File.Exists("constants.bin")) {
    GenerateConstants();

    Stream stream = new FileStream("constants.bin", FileMode.Create, FileAccess.Write, FileShare.None);
    new BinaryFormatter.Serialize(stream, constants);
    stream.Close();
}
else
{
    Stream stream = new FileStream("constants.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
    constants = (int[,])(new BinaryFormatter.Deserialize(stream));
    stream.Close();
}

, .

# "constants.bin" , 2D- . constants.bin 2D-.

+1

CSV - , , int double. .

+1
source

Pythonthe standard library includes sqlite3module - a lightweight disk-based database. For C#there are several libraries that provide support sqlite. For example, the System.Data.SQLitefull provider of ADO.NET 2.0 / 3.5.

For your application, use data types REAL(stored as an 8-byte IEEE floating-point number) and INTEGER(stored in 1, 2, 3, 4, 6, or 8 bytes depending on the value value).

+1
source

Source: https://habr.com/ru/post/1757005/


All Articles