简体   繁体   English

如何将 C# 字典传递给 Python 以及如何以 Python 可理解的格式获得相同的字典?

[英]How to pass C# Dictionary to Python and How can we get the same dictionary in python understandable format?

I am creating dictionary in C# like below.我正在 C# 中创建字典,如下所示。

IDictionary<int, string> dict = new Dictionary<int, string>(); // C# Code
dict.Add(1,"One");
dict.Add(2,"Two");
dict.Add(3,"Three");

Now how can i pass this 'dict' variable to python understandable format (python understand dictionary format).Or any other solution.现在我如何将这个“dict”变量传递给 python 可理解格式(python 理解字典格式)。或任何其他解决方案。

Basically i have to pass dictionary kind of thing to python from C# to get data in python.基本上我必须将字典类型的东西从 C# 传递给 python 才能在 python 中获取数据。

You can use serialization using standard format.您可以使用标准格式进行序列化。 For example JSON.例如 JSON。 Json.NET serialization for C# . C# 的 Json.NET 序列化 And pass the string to python.并将字符串传递给python。 In the python the should be another library to deserialize the string.在python中,应该是另一个反序列化字符串的库。 (This is the first link found while Googling "Python read JSON" ) 这是在谷歌搜索“Python 读取 JSON”时找到的第一个链接

It is not the fastest way, but it works.这不是最快的方法,但它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM