조회 수 38012 댓글 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 BeautifulSoup으로 웹에 있는 데이터 긁어오기 2013.04.08 76923 0
34 Python py2exe를 이용한 실행파일 만들기 2009.04.10 52821 0
33 Python Python Embedding (MFC프로그램에서 파이썬 실행하기) 2009.11.14 47678 0
32 Python python을 이용한 프로그램 개발 및 배포 방안 2010.02.25 46038 0
31 Python BeautifulSoup로 HTML 파싱 끝내기 2013.04.08 42527 0
» Python C#에서 Python 파일 실행하고 결과 가져오기 2012.02.02 38012 0
29 Python Google Colab에서 파일 업로드/다운로드 팁 2019.03.06 36406 0
28 Python python 문법요약 2013.04.08 28552 0
27 Python Image 기반 Steganography 예제 1 2019.07.17 25208 0
26 Python Python 문자열 관련 함수 2013.05.07 22571 0
25 Python zip() 함수 2014.04.30 22330 0
24 Python paramiko로 SSH 접속하기 2014.04.22 21503 0
23 Python Visual Studio 2005 + IronPython 연동하기 2012.02.02 16528 0
22 Python python 내장 함수 2014.04.30 16065 0
21 Python Python 유용한 코드 모음 2014.05.20 15131 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