1.c调用python:
实例代码:
main.c调用test.py的
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//main.c
#include <windows.h>
#include <python.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
Py_Initialize() ;//initialize the python system
PyObject * py= PyImport_ImportModule("test");
Py_Finalize();//Finalize the python system
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ......
1.c调用python:
实例代码:
main.c调用test.py的
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//main.c
#include <windows.h>
#include <python.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
Py_Initialize() ;//initialize the python system
PyObject * py= PyImport_ImportModule("test");
Py_Finalize();//Finalize the python system
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ......
最近使用python过程中,python界面的编程工具GTK-Python,但是界面的美观性不如Qt-Creator中的Qt-Designer,无法实现设计是视图绘制,有点让人失望。
网上发现有人介绍python Eric IDE,比较好奇,安装上看看吧:
#yum install eric4
但是新建的工程无法run,说缺少pyuic4,于是安装上开发包
#yum install PyQt4-devel
因为之前我按装了qt4.7类库,所以一切都那么顺其自然,新建了一个对话框,因为python eric 中嵌入了Qt -Designer工具,所以绘制的窗口比较美观,上档次,呵呵,不错,保存untitled.ui文件,关闭designer窗口,回到eric IDE 窗口发现在source视图下多了一个Ui_untitled.py文件:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '/home/fedora11/myPro/eric/untitled.ui'
#
# Created: Mon Mar 8 11:00:42 2010
# by: PyQt4 UI code gener ......
前几天,小许给我一份JavaQQ的源代码,用vim打开一看,发现里面的中文都是乱码。不用说,又是可恶的编码问题,在window下的文本文件通常使用GBK或GB18030编码,而在Linux下utf-8编码则大行其道。打开——另存为肯定不是上策,上网找编码批量转换工具也不是咱勤劳勇敢的程序员的作风。自已动手,丰衣足食,十几行Python代码解决问题。
#!/usr/bin/python3.1
import os
for path, folders, files in os.walk('.'):
for f in files:
if f.endswith('.java'):
try:
with open(os.path.join(path, f), encoding='gb18030') as src:
content = src.read()
with open(os.path.join(path, f),'wt', encoding='utf-8') as dest:
dest.write(content)
except:
continue
......
python的C、c++扩展
http://blog.chinaunix.net/u3/110228/showart_2148725.html
python的强大不仅表现在其功能上,而且还表现在其扩展能力上。
使用C/C++很容易编写python的模块,扩展python的功能。
同时将性能要求比较高的代码使用C/C++编写,能更好的弥补
脚本语言执行速度慢的缺陷。
1. python的C语言扩展
1.1 TestCLib.c: 提供python的模块接口
#include "Python.h"
#include <stdlib.h>
#include <stdio.h>
long fac(long);
// ------------------------------------------------------
// Make C code usable in Python
// ------------------------------------------------------
PyObject* TestCLib_fac(PyObject * self, PyObject *args)
{
long num;
if ( ! PyArg_ParseTuple(args,"l",&num)){
return NULL;
}
return (PyObject*)Py_BuildValue("l",fac(num));
}
static PyMethodDef TestCLibMethods[] = {
{"fac",TestCLib_fac,METH_VARARGS},
  ......
python的C、c++扩展
http://blog.chinaunix.net/u3/110228/showart_2148725.html
python的强大不仅表现在其功能上,而且还表现在其扩展能力上。
使用C/C++很容易编写python的模块,扩展python的功能。
同时将性能要求比较高的代码使用C/C++编写,能更好的弥补
脚本语言执行速度慢的缺陷。
1. python的C语言扩展
1.1 TestCLib.c: 提供python的模块接口
#include "Python.h"
#include <stdlib.h>
#include <stdio.h>
long fac(long);
// ------------------------------------------------------
// Make C code usable in Python
// ------------------------------------------------------
PyObject* TestCLib_fac(PyObject * self, PyObject *args)
{
long num;
if ( ! PyArg_ParseTuple(args,"l",&num)){
return NULL;
}
return (PyObject*)Py_BuildValue("l",fac(num));
}
static PyMethodDef TestCLibMethods[] = {
{"fac",TestCLib_fac,METH_VARARGS},
  ......
python的C、c++扩展
http://blog.chinaunix.net/u3/110228/showart_2148725.html
python的强大不仅表现在其功能上,而且还表现在其扩展能力上。
使用C/C++很容易编写python的模块,扩展python的功能。
同时将性能要求比较高的代码使用C/C++编写,能更好的弥补
脚本语言执行速度慢的缺陷。
1. python的C语言扩展
1.1 TestCLib.c: 提供python的模块接口
#include "Python.h"
#include <stdlib.h>
#include <stdio.h>
long fac(long);
// ------------------------------------------------------
// Make C code usable in Python
// ------------------------------------------------------
PyObject* TestCLib_fac(PyObject * self, PyObject *args)
{
long num;
if ( ! PyArg_ParseTuple(args,"l",&num)){
return NULL;
}
return (PyObject*)Py_BuildValue("l",fac(num));
}
static PyMethodDef TestCLibMethods[] = {
{"fac",TestCLib_fac,METH_VARARGS},
  ......
用python写的抓取天气预报的脚本
http://blog.chinaunix.net/u2/82009/showart_2166843.html
从昨天开始的看关于网络抓取的东西,而且自己的用的是awesome ,所以写了这个天气预报的脚本给我的awesome,这个天气脚本直接取下来的话是七天的天气预报从中国天气网上,我后面对它做了处理,用到了我的awesome上
效果:1日星期一夜间 阴 低温 4℃ 无持续风向 微风 | 2日星期二 小雨 --> 雨夹雪 3℃ --> 6℃ | 3日星期三 雨夹雪 1℃ --> 5℃
我只取了三天的预报,三天已经够了,下面程序的注释 英文实在有点过不了关
================================================
#!/usr/bin/env python
# weather html parser
from HTMLParser import HTMLParser
import sys,urllib2,string,re
# define a class to parser a html
class HtmlParser(HTMLParser):
def __init__(self):
self.data=''
self.readingdata=0
HTMLParser.__init__(self)
......
闲的无聊就看了一点关于python的基础知识,当时也不知道python和perl之间争论的这么的激烈(主要是当时不知道perl这个语言的性质),所以直接就看了python,下面是我的第一个用python写的小程序源码,希望朋友们多多指教,有什么问题大家尽管指正,在此先谢谢大家了。
[code]
#!/usr/bin/python
import sys, os, re
import pickle as p
class address:
def __init__(self,name,email,telephone):
self.name = name
self.email = email
self.telephone = telephone
def edit(self,new_name,new_email,new_telephone):
if len(new_name) != 0:
self.name = new_name
if len(new_email) != 0:
self.email = new_email
if len(new_telephone) != 0:
& ......