Hi,
How to get result in datatable in C# after run python script?
Hi,
How to get result in datatable in C# after run python script?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Uttam KumarPosted Feb 22, 2023, 3:30 PM
Amit, 2nd example shown by Tuhin shows how you can handle datatable returned by python script in c#.
Amit KumarPosted Feb 20, 2023, 8:35 AM
Thanks Tuhin,
But our python script return tabulate (like datatable) not in file with , seperated.
But read line read data from a file right.
Any idea for read data from datatable
Uttam KumarPosted Feb 17, 2023, 4:45 AM
Tuhin, very well explained with the example.
Tuhin PaulPosted Feb 16, 2023, 5:18 PM
Hi Amit, thanks for posting the question. This is a great learning experience to be able to answer this question. I hope the code below might answer your question.
To get the result of a Python script in a C# DataTable, see below:
1. Create a new Python script that returns the result as a CSV file or a JSON file. For example, you can use the csv module in Python to write the result to a CSV file:
2. Call the Python script from C# using the Process class. Pass any necessary arguments to the Python script using the Arguments property of the ProcessStartInfo object. In this example, we assume that the Python script is named script.py and is located in the same directory as the C# code, this code calls a Python script and stores the result in a DataTable:
In this code, we assume that the Python script returns the result as a CSV file with two columns: "Name" and "Age". The C# code reads the output of the Python script and stores it in a DataTable with the same columns. Finally, the code prints the DataTable to the console.
You can modify the code to write the DataTable to a file, display it in a UI, or perform any other operation as needed. Let me know if it helps you in a way. Good Luck.