조회 수 38146 댓글 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
번호 분류 제목 날짜 조회 수 추천 수
35 Python pyBest 소스 secret 2016.01.20 0 0
34 Python pandas, matplot 자주사용하는 코드 2019.03.06 4993 0
33 Python [tensorflow] 텐서플로우 문서 한글번역본 2018.03.22 5238 0
32 Python [tensorflow] 선형회귀 예제 2018.02.05 5451 0
31 Python 디렉토리 없으면 만들기 2019.03.30 5885 0
30 Python matplot에서 한글이 보이도록 하는 코드 2019.03.06 6137 0
29 Python 줄 바꿈 없이 출력하는 방법 2019.03.30 6966 0
28 Python [Sconscript] Install method 2014.05.29 8580 0
27 Python json 데이터 핸들링 2017.03.09 8732 0
26 Python distutils 개요 2014.04.24 9460 0
25 Python pylab - Plotting with Matplotlib 2014.04.28 9807 0
24 Python pygoogle 파이썬으로 구글 검색결과 가져오기 library 2016.01.20 9929 0
23 Python map() 함수 2014.04.30 9989 0
22 Python wxPython Tutorial 링크 2014.04.24 10645 0
21 Python 커맨드 라인에서 컬러로 출력하기 termcolor 2014.06.27 10649 0
목록
Board Pagination ‹ Prev 1 2 3 Next ›
/ 3

나눔글꼴 설치 안내


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

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

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5