Python 读文件代码示例

程序功能:
python 打开一个文件,按行读取分拆数据,对数据进行格式转换,存储到list中

#打开文件
with open(optns['PSASP_FILE']+'\LF.L1','r') as fid:
    textLines = fid.read().splitlines();#按行取内容
for row in range(0,len(textLines)):#对每一行进行提取内容
    ts = textLines[row].split(',');#按‘,’进行分拆
    idbus = row+1;#as row starts from 0
    Bus_Name = ts[0].replace("'","").strip();#替换‘‘’同时去掉空格
    busNamemap[idbus] = Bus_Name;
    Vbase = float(ts[1]);#数据类型转换
    Area  = int(ts[2]);
    Vmax  = float(ts[3]);
    Vmin  = float(ts[4]);
    CB1   = float(ts[5]);
    CB3   = float(ts[6]);
    bustype = 1; #initialized 1 and modified by load and generator data. 
    serv = 0;#flag of whether in service
    V0 = 1.0;# initial voltage bus
    Ang0 = 0.0;
    busMap[idbus] = len(busData);
    #               0      1         2      3     4     5     6    7    8         9       10   11 12
    busData.append([idbus, Bus_Name, Vbase, Area, Vmax, Vmin, CB1, CB3, Bus_Name, bustype,serv,V0,Ang0]);#数据存储
if optns['OUTPUT_LEVEL'] > 2:
    PrintT(optns,'Finish to read Bus info, Start to read AC Line...');
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值