PythonNET DateTime C# .NET List 如何填充它?
PythonNET DateTime C# .NET List how to populate it?
我找不到有关 PythonNET 使用列表的任何答案,列表是 Python 的一个包,可让您导入 .NET DLL。出于某种原因,创建 DateTime 列表对我不起作用。 已解决以下是如何让它工作:
import clr
from pandas import to_datetime, Series
from System.Collections.Generic import List
from System import DateTime
Contracts = to_datetime(Series(['9/1/2014','10/1/2014','11/1/2014','12/1/2014','1/1/2015','2/1/2015','3/1/2015','4/1/2015','5/1/2015','6/1/2015','7/1/2015','8/1/2015']))
DateList = List[DateTime](range(len(Contracts)))
for i in range(len(Contracts)): DateList.Add(DateTime(Contracts[i].year,Contracts[i].month,Contracts[i].day))
(抱歉上面的格式,我无法在循环中使用缩进)。要显示列表的内容,您可以这样做:
print(DateList.get_Item(0))
9/1/2014 12:00:00 AM
print(DateList.get_Item(11))
8/1/2015 12:00:00 AM
已解决以下是如何让它发挥作用:
import clr
from pandas import to_datetime, Series
from System.Collections.Generic import List from System import DateTime
Contracts = to_datetime(Series(['9/1/2014','10/1/2014','11/1/2014','12/1/2014','1/1/2015','2/1/2015','3/1/2015','4/1/2015','5/1/2015','6/1/2015','7/1/2015','8/1/2015']))
DateList = List[DateTime](range(len(Contracts)))
for i in range(len(Contracts)): DateList.Add(DateTime(Contracts[i].year,Contracts[i].month,Contracts[i].day))
(抱歉上面的格式,我无法在循环中使用缩进)。要显示列表的内容,您可以这样做:
print(DateList.get_Item(0))
9/1/2014 12:00:00 AM
print(DateList.get_Item(11))
8/1/2015 12:00:00 AM
此示例代码展示了如何将整数、列表和 DateTime 从 Python 传递到 C# 并再次返回。
很容易直接进入 DateTimes 列表,但在尝试@Matt 的回答之前先让一些简单的工作有用(感谢您的出色回答!)。
测试:
- Python 3.7.3
- pythonnet 2.4dev0 来自 https://www.lfd.uci.edu/~gohlke/pythonlibs/
- 截至2019-04-12,Windows wheel尚未在PyPi上发布,将在一周左右发布。
- .NET 4.7.2.
- 还使用 .NET Standard 2.0 进行了测试,效果一样好。
- 我假设在发布时与 .NET Standard 3.0 兼容。
C#代码,创建Class项目后编译:
// Compile as "x32" or "x64" (depending on the bitness of your Python), but not "Any CPU".
using System;
namespace NetClass
{
public class MyClass
{
public int XPlusOne(int x)
{
return x + 1;
}
public int[] XTimesTwo(int[] x)
{
int[] result = new int[x.Length];
for (int i = 0; i < x.Length; i++)
{
result[i] = x[i] * 2;
}
return result;
}
public DateTime DatePlusOne(DateTime dateTime)
{
return dateTime.AddDays(1);
}
}
}
Python:
import clr
# Must compile your C# .dll in x64 or x32 (but not "Any CPU"), depending on the bitness of Python.
clr.AddReference('C:\python\NetClass\NetClass\bin\x64\Release\NetClass.dll')
# Example above is for .NET 4.7.2. Same thing for .NET Standard 2.0.
from NetClass import MyClass
myClass = MyClass()
x_plus_one = myClass.XPlusOne(4)
print(x_plus_one) # Prints 5
x_array = [1, 2, 3, 4]
x_array_times_two = list(myClass.XTimesTwo(x_array))
print(x_array_times_two) # Prints [ 2,4,6,8 ]
import datetime as dt
from System import DateTime
date = dt.date(year=2019, month=4, day=12)
d = myClass.DatePlusOne(DateTime(date.year, date.month, date.day))
date_plus_one = dt.date(d.Year, d.Month, d.Day)
print(date_plus_one) # Prints 2019-04-13.
输出:
5 # Input into C# is a python int of 4
[2, 4, 6, 8] # Input into C# is a python array [1,2,3,4]
2019-04-13 # Input into C# is a python date 2019-04-12
我找不到有关 PythonNET 使用列表的任何答案,列表是 Python 的一个包,可让您导入 .NET DLL。出于某种原因,创建 DateTime 列表对我不起作用。 已解决以下是如何让它工作:
import clr
from pandas import to_datetime, Series
from System.Collections.Generic import List
from System import DateTime
Contracts = to_datetime(Series(['9/1/2014','10/1/2014','11/1/2014','12/1/2014','1/1/2015','2/1/2015','3/1/2015','4/1/2015','5/1/2015','6/1/2015','7/1/2015','8/1/2015']))
DateList = List[DateTime](range(len(Contracts)))
for i in range(len(Contracts)): DateList.Add(DateTime(Contracts[i].year,Contracts[i].month,Contracts[i].day))
(抱歉上面的格式,我无法在循环中使用缩进)。要显示列表的内容,您可以这样做:
print(DateList.get_Item(0))
9/1/2014 12:00:00 AM
print(DateList.get_Item(11))
8/1/2015 12:00:00 AM
已解决以下是如何让它发挥作用:
import clr
from pandas import to_datetime, Series
from System.Collections.Generic import List from System import DateTime
Contracts = to_datetime(Series(['9/1/2014','10/1/2014','11/1/2014','12/1/2014','1/1/2015','2/1/2015','3/1/2015','4/1/2015','5/1/2015','6/1/2015','7/1/2015','8/1/2015']))
DateList = List[DateTime](range(len(Contracts)))
for i in range(len(Contracts)): DateList.Add(DateTime(Contracts[i].year,Contracts[i].month,Contracts[i].day))
(抱歉上面的格式,我无法在循环中使用缩进)。要显示列表的内容,您可以这样做:
print(DateList.get_Item(0))
9/1/2014 12:00:00 AM
print(DateList.get_Item(11))
8/1/2015 12:00:00 AM
此示例代码展示了如何将整数、列表和 DateTime 从 Python 传递到 C# 并再次返回。
很容易直接进入 DateTimes 列表,但在尝试@Matt 的回答之前先让一些简单的工作有用(感谢您的出色回答!)。
测试:
- Python 3.7.3
- pythonnet 2.4dev0 来自 https://www.lfd.uci.edu/~gohlke/pythonlibs/
- 截至2019-04-12,Windows wheel尚未在PyPi上发布,将在一周左右发布。
- .NET 4.7.2.
- 还使用 .NET Standard 2.0 进行了测试,效果一样好。
- 我假设在发布时与 .NET Standard 3.0 兼容。
C#代码,创建Class项目后编译:
// Compile as "x32" or "x64" (depending on the bitness of your Python), but not "Any CPU".
using System;
namespace NetClass
{
public class MyClass
{
public int XPlusOne(int x)
{
return x + 1;
}
public int[] XTimesTwo(int[] x)
{
int[] result = new int[x.Length];
for (int i = 0; i < x.Length; i++)
{
result[i] = x[i] * 2;
}
return result;
}
public DateTime DatePlusOne(DateTime dateTime)
{
return dateTime.AddDays(1);
}
}
}
Python:
import clr
# Must compile your C# .dll in x64 or x32 (but not "Any CPU"), depending on the bitness of Python.
clr.AddReference('C:\python\NetClass\NetClass\bin\x64\Release\NetClass.dll')
# Example above is for .NET 4.7.2. Same thing for .NET Standard 2.0.
from NetClass import MyClass
myClass = MyClass()
x_plus_one = myClass.XPlusOne(4)
print(x_plus_one) # Prints 5
x_array = [1, 2, 3, 4]
x_array_times_two = list(myClass.XTimesTwo(x_array))
print(x_array_times_two) # Prints [ 2,4,6,8 ]
import datetime as dt
from System import DateTime
date = dt.date(year=2019, month=4, day=12)
d = myClass.DatePlusOne(DateTime(date.year, date.month, date.day))
date_plus_one = dt.date(d.Year, d.Month, d.Day)
print(date_plus_one) # Prints 2019-04-13.
输出:
5 # Input into C# is a python int of 4
[2, 4, 6, 8] # Input into C# is a python array [1,2,3,4]
2019-04-13 # Input into C# is a python date 2019-04-12