조회 수 38141 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print


출처 : http://taesudo.blog.me/120069564847


 

저는 Pyhon을 좋아하는데 GUI 때문에 C#을 사용하는 경우가 많습니다.

그러다보니 Python으로 짠 것을 다시 C#으로 변경하는 일이 종종 있었죠.

그게 통 맘에 안 들었는데 이번에 좋은 방법을 찾아냈습니다.

개발환경: Visual Studio 2008에서 C# 사용, IronPython2.0 설치

C# 파일: (참조에서 IronPython을 먼저 참조추가해야 합니다.)

----------------------------------

using IronPython.Hosting; //파이선을 실행하기 위해 추가

class getPython
{
 static void Main(string[] args)
 {
  //먼저 참조추가에서 IronPython을 추가하야 한다.
  //물론 그 전에 IronPython이 설치되어 있어야 겠지. (현재는 2.0버젼 2009.6.4)
  PythonEngine python = new PythonEngine();

  //파이선 명령어 실행
  python.Execute("res = 10");
  object result = python.Globals["res"]; //결과를 받음
  Console.WriteLine("result: {0}", result.ToString());

  int number = 50;

  python.Globals["number"] = number; //파이선에서 사용할 변수의 초기값 지정하여 저장

  try
  {
   //파이선 프로그램 파일 실행.
   python.ExecuteFile("../../simple.py");
  }
  catch (Exception ex)
  {
   Console.WriteLine(ex.Message);
  }
  result = python.Globals["result"];
  Console.WriteLine("result: {0}", result.ToString());

  IronPython.Runtime.List result2 = (IronPython.Runtime.List)python.Globals["listA"]; // IronPython.Runtime.List
  Console.WriteLine("result: {0}", result2.GetLength());
  string strA = result2.ToCodeString();

  //리스트를 가져와서 보여주자.
  foreach(int aa in result2){
   Console.WriteLine("result: {0} {1}", strA, aa);
  }
  //사전도 가져와서 보여주자.
  IronPython.Runtime.Dict result3 = (IronPython.Runtime.Dict)python.Globals["dictA"]; // IronPython.Runtime.List
  Console.WriteLine("result: {0}", result3.GetLength());
  foreach (string aa in result3)
  {
   Console.WriteLine("result: {0} {1}",aa, result3[aa]);
  }
  Console.ReadLine();
 }
}

__________________________________

#파이선 파일(simple.py)

def Divide(number):
    return number/2

result = Divide(number)listA = [10,20,30,40,50]dictA = {}
dictA['우리나라'] = '대한민국'
dictA['중국'] = 'china'
dictA['미국'] = 'USA'
dictA['일본'] = 'Japan'
dictA['영국'] = 'England'


VS2008에서 실행해 보세요. 잘 될 것입니다. 파이선에서 만든 리스트나 사전을 C#에서 쉽게

불러올 수 있네요. 이제 파이선으로 핵심 프로그램은 짠 다음 C#에서는 그 결과만 보여주면 되네요.

http://blog.naver.com/drmstorm/50015791071 여기서 힌트를 얻었습니다. 감사^^^


Dreamy의 코드 스크랩

내가 모으고 내가 보는

List of Articles
번호 분류 제목 날짜 조회 수 추천 수
446 C# byte[] <-> string 변환 방법 2013.01.16 41047 0
445 C# C# ?? 및 ??=, ?. 연산자 2021.05.09 3152 0
444 C# C# Color Table 2012.12.04 18821 0
443 C# c# file i/o 샘플 2012.01.04 16993 0
442 C# C# JSON 파싱(parsing): Newtonsoft 및 System.Text.Json 2021.05.15 11047 0
441 C# C# XML Documentation - C# 소스코드 문서화 [출처] C# XML Documentation - C# 소스코드 문서화 2013.10.01 14659 0
440 C# c# 다국어 개발 2012.05.22 18158 0
439 C# C# 레지스트리 사용하기 2010.03.15 40501 0
438 C# C# 문자열 숫자형식 포맷팅 String.Format ( C# Numberic Formatting ) 2012.10.10 59404 0
437 C# C# 배열 array 2014.10.22 12594 0
436 C# C# 시스템 정보 가져오기 2013.01.18 22167 0
435 C# C# 자주 쓰는 코드 2012.12.10 11974 0
434 C# C# 코드에서 cmd 명령어기 날리기, 리디렉션하기 2012.09.03 34331 0
433 C# C# 형변환 (문자형 -> 숫자형) / C# Type Change (string -> number) 2012.04.12 57356 0
» Python C#에서 Python 파일 실행하고 결과 가져오기 2012.02.02 38141 0
목록
Board Pagination ‹ Prev 1 2 3 4 5 6 7 8 9 10 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5