I am using .net 4.7.1 console program talking to python.net that VS2017 is reporting as version 2.5.1.0 (runtime version v4.0.30319) Python code is in 3.6
python:
JavaScript
x
16
16
1
def ping(input):
2
if (input == 'ping'):
3
return 'pong'
4
return 'invalid'
5
6
def headervalid(header):
7
if (header == '@nu001erANSI '):
8
return True
9
return False
10
11
if __name__ == '__main__':
12
input = '@nu001erANSI '
13
print(headervalid(input))
14
input = 'ping'
15
print(ping(input))
16
dot net :
JavaScript
1
32
32
1
using (Py.GIL())
2
{
3
dynamic np = Py.Import("numpy");
4
Console.WriteLine(np.cos(np.pi * 2));
5
6
dynamic sin = np.sin;
7
Console.WriteLine(sin(5));
8
9
double c = np.cos(5) + sin(5);
10
Console.WriteLine(c);
11
12
dynamic a = np.array(new List<float> { 1, 2, 3 });
13
Console.WriteLine(a.dtype);
14
15
dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);
16
Console.WriteLine(b.dtype);
17
18
Console.WriteLine(a * b);
19
20
dynamic parsers = Py.Import("newworld_parsers.bridgetest");
21
22
string input = "ping";
23
var result = parsers.ping(input);
24
Console.WriteLine(result);
25
input = @"@nu001erANSI ";
26
result = parsers.headervalid(input);
27
Console.WriteLine(result);
28
29
Console.WriteLine("=======");
30
Console.ReadLine();
31
}
32
The python stand alone run reports:
JavaScript
1
4
1
True
2
pong
3
Press any key to continue . . .
4
Dot net run reports:
JavaScript
1
10
10
1
1.0
2
-0.9589242746631385
3
-0.675262089199912
4
float64
5
int32
6
[ 6. 10. 12.]
7
pong
8
False
9
=== Press any key to continue ====
10
Notice the True in python vs the False when calling from C# The special characters in headervalid() from dot net don’t seem to be going over correctly. What should I do to fix this? Any ideas greatly appreciated!
Advertisement
Answer
Putting ‘@’ character in front of C# string turns it into a raw string, meaning no escape sequences inside will work.
You can see that by adding Console.WriteLine(input);
to your code.