matlab fscanf %lg,python 没有了matlab的fscanf功能,我该怎么办

The data file

.... # unknow lines

tag

134

345

256

2345

234

othertag

The matlab solution

%matlab

% to reading the data from the above format file

% if using matlab, it is easy

fid = fopen(filename);

tag1 = 'tag';

while 1

strline = fgetl(fid);

if strcmp('tag',strline) == 1

result = fscanf(fid,'%f',[1,inf]);

fclose(fid);

break;

end

end

The function "fscanf" makes the process very easy.

Today, I want to use python to realize the same function, but a problem was met: how to read the data in one time?

My python solution

The first time

filename = 'test.txt'

startrecord = False

result = []

with open(filename) as f:

for line in f:

line = line.strip()

if line.startswith('tag'):

startrecord = True

continue

if not line:

startrecord = False

if startrecord:

result.append(float(line))

f.close()

print result

[345.0, 2345.0, 234.0]

The new way

Wait for other resolutions

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值