python最简单的从文件中读取数据的方式是什么?
-
- 帖子: 294
- 注册时间: 2007-08-26 10:35
- 来自: 上海
- 联系:
python最简单的从文件中读取数据的方式是什么?
需求是从一个配置文件中读取结构化的数据。
php的一种方式是将数据文件写为返回一个php数组的方式。工作文件只需要include这个数据文件就可以直接使用这个php数据。
不知道python有没有类似这种简单的方法?
php的一种方式是将数据文件写为返回一个php数组的方式。工作文件只需要include这个数据文件就可以直接使用这个php数据。
不知道python有没有类似这种简单的方法?
-
- 帖子: 3182
- 注册时间: 2006-03-10 15:10
- 来自: xi'an China
- 联系:
-
- 帖子: 294
- 注册时间: 2007-08-26 10:35
- 来自: 上海
- 联系:
能不能说的具体一点,比如说php中的例子:cozo 写了:脚本语言都有动态运行的能力,你把配置文件存成Python的代码,import一下不就得了,等于PHP的Include。
a.php:
代码: 全选
<?php
return array(
array('name' => 'abc', 'code' => 12),
array('name' => 'xyz', 'code' => 34)
);
代码: 全选
<?php
$list = include('a.php');
print_r($list);
代码: 全选
Array
(
[0] => Array
(
[name] => abc
[code] => 12
)
[1] => Array
(
[name] => xyz
[code] => 34
)
)
- SuperWar3Fan
- 帖子: 1263
- 注册时间: 2006-05-20 6:25
- 来自: 山东淄博
- 联系:
- anticlockwise
- 帖子: 2394
- 注册时间: 2007-03-01 20:46
- 来自: 湖南长沙
- frank7258
- 帖子: 61
- 注册时间: 2007-01-31 23:01
- 来自: 慕尼黑
- 联系:
代码: 全选
#!/usr/bin/python
# Filename: pickling.py
import cPickle as p
#import pickle as p
shoplistfile = 'shoplist.data'
# the name of the file where we will store the object
shoplist = ['apple', 'mango', 'carrot']
# Write to the file
f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()
del shoplist # remove the shoplist
# Read back from the storage
f = file(shoplistfile)
storedlist = p.load(f)
print storedlist