python
Aug
11
突然想写个Python读Rss的程序看看。
代码
----------------------------------------------------------
#!d:\Python24\python.exe
#-*- encoding: gb2312 -*-
import os, sys, string
import feedparser
furl='http://www.gjjblog.com/feed.php'
#模拟浏览器
feedparser.USER_AGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
try:
d = feedparser.parse(furl)
s= d.encoding
print d.feed.title.encode(s)
j=len(d['items'])
print j
for r in range(j):
e=d['items'][r]
xtitle=e['title']
print e['link']
print e['description']
except Exception, e:
print "错误 => "+str(e)
代码
----------------------------------------------------------
#!d:\Python24\python.exe
#-*- encoding: gb2312 -*-
import os, sys, string
import feedparser
furl='http://www.gjjblog.com/feed.php'
#模拟浏览器
feedparser.USER_AGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
try:
d = feedparser.parse(furl)
s= d.encoding
print d.feed.title.encode(s)
j=len(d['items'])
print j
for r in range(j):
e=d['items'][r]
xtitle=e['title']
print e['link']
print e['description']
except Exception, e:
print "错误 => "+str(e)
Jun
6
用python读股票文件都几方便,看来可以用python构造自己的交易程序了,
以下是读文件和生成移动平均线值的python程序
#-*- encoding: gbk -*-
from struct import *
import time
# 读取股票文件, 使用是大智慧股票软件的数据文件格式
fd=file("500018.day",'rb')
# 读取文件到数组xx
xx=[]
try:
buf = fd.read()
icount=len(buf)/40
k=0
while True:
j=k*40
(a1,a2,a3,a4,a5,a6,a7)=unpack('IIIIIII',buf[j:(j+28)])
idate=a1
iopen=float(a2) / 1000
ihigh=float(a3) / 1000
ilow=float(a4) / 1000
iclose=float(a5) / 1000
ivol=a6
# 一列的数组z
z=[idate,iopen,ihigh,ilow,iclose,ivol,a7]
# 一列数据再插入数组xx
xx.append(z)
k=k+1
if k==icount:
break
finally:
fd.close;
# 位置是结尾
weizhi=len(xx)-1
# 周期 5
zou=5
# zoux是周期 weizhix是计算位置 data是输入数组文件
def NewMa(zoux,weizhix,data):
zclose=0
aclose=0
for i in range(0,zoux):
aa=data[weizhix-i]
# print aa
aclose=aa[4]
zclose=zclose+aclose
maclose=zclose / zou
return maclose
# 显示 5日均线的值
print NewMa(zou,weizhi,xx)
以下是读文件和生成移动平均线值的python程序
#-*- encoding: gbk -*-
from struct import *
import time
# 读取股票文件, 使用是大智慧股票软件的数据文件格式
fd=file("500018.day",'rb')
# 读取文件到数组xx
xx=[]
try:
buf = fd.read()
icount=len(buf)/40
k=0
while True:
j=k*40
(a1,a2,a3,a4,a5,a6,a7)=unpack('IIIIIII',buf[j:(j+28)])
idate=a1
iopen=float(a2) / 1000
ihigh=float(a3) / 1000
ilow=float(a4) / 1000
iclose=float(a5) / 1000
ivol=a6
# 一列的数组z
z=[idate,iopen,ihigh,ilow,iclose,ivol,a7]
# 一列数据再插入数组xx
xx.append(z)
k=k+1
if k==icount:
break
finally:
fd.close;
# 位置是结尾
weizhi=len(xx)-1
# 周期 5
zou=5
# zoux是周期 weizhix是计算位置 data是输入数组文件
def NewMa(zoux,weizhix,data):
zclose=0
aclose=0
for i in range(0,zoux):
aa=data[weizhix-i]
# print aa
aclose=aa[4]
zclose=zclose+aclose
maclose=zclose / zou
return maclose
# 显示 5日均线的值
print NewMa(zou,weizhi,xx)
Mar
4
记录一下,以后应该要用到的
os和os.path模块
os.listdir(dirname):列出dirname下的目录和文件
os.getcwd():获得当前工作目录
os.curdir:返回但前目录('.')
os.chdir(dirname):改变工作目录到dirname
os.path.isdir(name):判断name是不是一个目录,name不是目录就返回false
os.path.isfile(name):判断name是不是一个文件,不存在name也返回false
os.path.exists(name):判断是否存在文件或目录name
os.path.getsize(name):获得文件大小,如果name是目录返回0L
os.path.abspath(name):获得绝对路径
os.path.normpath(path):规范path字符串形式
os.path.split(name):分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)
os.path.splitext():分离文件名与扩展名
os.path.join(path,name):连接目录与文件名或目录
os.path.basename(path):返回文件名
os.path.dirname(path):返回文件路径
>>> import os
>>> os.getcwd()
'C:\\Python25'
>>> os.chdir(r'C:\temp')
>>> os.getcwd()
'C:\\temp'
>>> os.listdir('.')
['temp.txt', 'test.py', 'testdir', 'tt']
>>> os.listdir(os.curdir)
['temp.txt', 'test.py', 'testdir', 'tt']
>>> os.path.getsize('test.py')
38L
>>> os.path.isdir('tt')
True
>>> os.path.getsize('tt')
0L
>>> os.path.abspath('tt')
'c:\\temp\\tt'
>>> os.path.abspath('test.py')
'c:\\temp\\test.py'
>>> os.path.abspath('.')
'c:\\temp'
>>>
>>> os.path.split(r'.\tt')
('.', 'tt')
>>> os.path.split(r'c:\temp\test.py')
('c:\\temp', 'test.py')
>>> os.path.split(r'c:\temp\test.dpy')
('c:\\temp', 'test.dpy'
>>> os.path.splitext(r'c:\temp\test.py')
('c:\\temp\\test', '.py')
>>> os.path.splitext(r'c:\temp\tst.py')
('c:\\temp\\tst', '.py')
>>>
>>> os.path.basename(r'c:\temp\tst.py')
'tst.py'
>>> os.path.dirname(r'c:\temp\tst.py')
'c:\\temp'
>>>
这些python关于目录和文件操作的方法,,都要靠在网上找到,python好像没有类似php和perl的手册,有这些手册就方便点了
os和os.path模块
os.listdir(dirname):列出dirname下的目录和文件
os.getcwd():获得当前工作目录
os.curdir:返回但前目录('.')
os.chdir(dirname):改变工作目录到dirname
os.path.isdir(name):判断name是不是一个目录,name不是目录就返回false
os.path.isfile(name):判断name是不是一个文件,不存在name也返回false
os.path.exists(name):判断是否存在文件或目录name
os.path.getsize(name):获得文件大小,如果name是目录返回0L
os.path.abspath(name):获得绝对路径
os.path.normpath(path):规范path字符串形式
os.path.split(name):分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)
os.path.splitext():分离文件名与扩展名
os.path.join(path,name):连接目录与文件名或目录
os.path.basename(path):返回文件名
os.path.dirname(path):返回文件路径
>>> import os
>>> os.getcwd()
'C:\\Python25'
>>> os.chdir(r'C:\temp')
>>> os.getcwd()
'C:\\temp'
>>> os.listdir('.')
['temp.txt', 'test.py', 'testdir', 'tt']
>>> os.listdir(os.curdir)
['temp.txt', 'test.py', 'testdir', 'tt']
>>> os.path.getsize('test.py')
38L
>>> os.path.isdir('tt')
True
>>> os.path.getsize('tt')
0L
>>> os.path.abspath('tt')
'c:\\temp\\tt'
>>> os.path.abspath('test.py')
'c:\\temp\\test.py'
>>> os.path.abspath('.')
'c:\\temp'
>>>
>>> os.path.split(r'.\tt')
('.', 'tt')
>>> os.path.split(r'c:\temp\test.py')
('c:\\temp', 'test.py')
>>> os.path.split(r'c:\temp\test.dpy')
('c:\\temp', 'test.dpy'
>>> os.path.splitext(r'c:\temp\test.py')
('c:\\temp\\test', '.py')
>>> os.path.splitext(r'c:\temp\tst.py')
('c:\\temp\\tst', '.py')
>>>
>>> os.path.basename(r'c:\temp\tst.py')
'tst.py'
>>> os.path.dirname(r'c:\temp\tst.py')
'c:\\temp'
>>>
这些python关于目录和文件操作的方法,,都要靠在网上找到,python好像没有类似php和perl的手册,有这些手册就方便点了
Mar
3
昨天晚上在window用py2exe编译python写的程序,由于第一次用,好多不解,当编译出现The following modules appear to be missing错误时,就唔知怎样处理了, 以下为错误信息:
The following modules appear to be missing
['DateTime', '_ssl', 'chardet', 'chardet.constants', 'cjkcodecs.aliases', 'iconv_codec', 'mx.DateTime', 'mx.Tidy', 'tidy']
在网上google后,才知道要在配置文件动手脚,以下我的配置文件
# setup.py
from distutils.core import setup
setup(console=["getrssgb.py"],
options = {'py2exe': {'excludes': ['DateTime','_ssl','chardet', 'chardet.constants', 'cjkcodecs.aliases', 'iconv_codec', 'mx.DateTime', 'mx.Tidy', 'tidy']}}
)
这样编译就没问题了.
The following modules appear to be missing
['DateTime', '_ssl', 'chardet', 'chardet.constants', 'cjkcodecs.aliases', 'iconv_codec', 'mx.DateTime', 'mx.Tidy', 'tidy']
在网上google后,才知道要在配置文件动手脚,以下我的配置文件
# setup.py
from distutils.core import setup
setup(console=["getrssgb.py"],
options = {'py2exe': {'excludes': ['DateTime','_ssl','chardet', 'chardet.constants', 'cjkcodecs.aliases', 'iconv_codec', 'mx.DateTime', 'mx.Tidy', 'tidy']}}
)
这样编译就没问题了.




