@huyl08
2016-08-27T14:28:59.000000Z
字数 2852
阅读 1621
Python
星迹迷航
文件读写
- 目前Python主要有2.7.x和3.x和两个分支版本,可前往Python官网下载最新版本的Python。本版本的“新手上路”以Python2.7.12作为演示样例
- 基本的操作可学习官网新手入门(英语)、Codecademy(英语)、Python 基础教程(中文)或其他类似资源。
- 若Python源文件中存在中文内容,需在文件的最上端顶文件头加入代码片段"# encoding:utf-8"的内容使得Python运行时环境能够识别中文。
- 完整的Python示例源码可通过该链接下载
下载Windows下最新版本的Python安装包python-2.7.12.msi,双击安装,根据自己需要设置安装位置以及环境变量,在Path环境变量中添加Python的主目录位置(默认为C:\Python27\,下图界面中勾选"Add python.exe to Path"会默认添加环境变量)
python -V
正确的版本输出为
Python 2.7.12
以下为Python源码样例
# 将输入的字符串转化为坐标数组
# 参数: geometry_string 记载几何形态的字符串
# 返回值: 记录坐标元组(tuple)的数组(list)
def geometry_parse(geometry_string):
# 声明返回值数组
results = []
# 按“;”分隔字符串
tokens = geometry_string.split(";")
for token in tokens:
parts = token.split(":")
x = float(parts[0])
y = float(parts[1])
results.append((x, y))
return results
# 读文件函数
# 参数: file_path为输入文件路径
# 返回值: 返回路段属性字典(dict)
def readFile(file_path):
# 声明返回值字典
results = {}
# 打开文件
fin = open(file_path, "r")
# 遍历文件的每一行,line为一行字符串
for line in fin:
if len(line) < 1:
continue
# 按逗号分割列
tokens = line.strip().split(",")
link_id = int(tokens[0])
from_node = int(tokens[1])
to_node = int(tokens[2])
link_length = float(tokens[3])
link_class = int(tokens[4])
link_geom = tokens[5]
# 按 LinkID : Link属性 的形式向results里追加路段信息
results.setdefault(link_id, (from_node, to_node, link_length, link_class, geometry_parse(link_geom)))
# 关闭文件
fin.close()
# 返回结果
return results
class GPS:
# 构造方法,将文本行转化为GPS类实例
def __init__(self, line):
# 按逗号分割列
items = line.strip().split(",")
self.vid = items[0]
# 将yyyyMMddHHmmss格式的字符串转化为datetime对象
self.datetime = datetime.strptime(items[1], "%Y%m%d%H%M%S")
self.x = float(items[2])
self.y = float(items[3])
self.speed = float(items[4])
self.direction = float(items[5])
# 将当前实例转化为字符串
def __str__(self):
return "%s\t%s\t(%.6f, %.6f)" % (self.vid, self.datetime.strftime("%Y-%m-%d %H:%M:%S"), self.x, self.y)
# 求和另一条记录之间的时间差
def time_difference(self, another):
return ( another.datetime - self.datetime ).total_seconds()
# 求和另一条记录之间的距离
# 注意:实际计算中需要做经纬度到以米为单位的投影转换
def distance(self, another):
dx = another.x - self.x
dy = another.y - self.y
return math.sqrt( dx * dx + dy * dy )
Python调试常用pdb模块来进行分析基本操作如下
import pdb
for i in range(0, 100):
print "Seed is %d" % (i)
pdb.set_trace()
print "Double value is %d " % ( 2 * i )
执行该脚本之后,则会在pdb.set_trace()的位置停顿,可输入不同的命令进行交互式调试,常用命令如下
s(tep): 让程序运行下一行,如果当前有一个函数调用,那么s会进入被调用的函数体中
n(ext): 让程序运行下一行,如果当前有一个函数调用,用n是不会进入被调用的函数体中的
c(ontinue): 让程序正常运行,直到遇到断点
l(ist): 打印当前位置的上下文代码
p(rint) variable: 打印某个变量的值
1,3,4,280.3,2,115.488988:39.917917;115.488997:39.917938;115.489045:39.918061;115.489219:39.918316;115.489335:39.918486;115.489366:39.918540;115.489382:39.918612;115.489385:39.918672;115.489373:39.918713
3,7,8,173.2,2,115.381547:39.950776;115.381786:39.950461;115.381848:39.950355;115.381887:39.950260;115.381913:39.950163
57952495354,20160701120100,116.372966,39.908912,26.00,262
13381083463,20160701120147,116.387472,39.889589,26.00,266
13311492120,20160701120144,116.355589,39.993436,10.00,260
13439610594,20160701120147,116.394988,39.940625,0.00,0