python中如何使两行做差

onizuka0912 2008-05-04 07:04:13
一个.txt文档,里面的数据如下:
abc 1,208,757,887,678,275
def 1,208,757,887,678,291

文件里有多组上面这样的数据,请问如何使其中的数值做差并保存?至于字符串结果无所谓~谢谢!
...全文
3746 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
boyz2men 2008-05-09
  • 打赏
  • 举报
回复
xy=0
list=[]
for row in open('txt'):
list.append(float(row.split()[-1])-xy)
xy=row
boyz2men 2008-05-09
  • 打赏
  • 举报
回复
好麻烦

arrary.fromlist fromfile fromstr
then arrary[:-1]-arrary[0:]

搞哪么复杂干吗???
lixq2000 2008-05-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 iambic 的回复:]
楼主还是举个例子吧。比如文件有4行,希望得出来的结果是什么?
[/Quote]给出一个实际的例子,也好写程序!
iambic 2008-05-04
  • 打赏
  • 举报
回复
楼主还是举个例子吧。比如文件有4行,希望得出来的结果是什么?
DDGG 2008-05-04
  • 打赏
  • 举报
回复
# -*- coding=GBK -*-

# 假设你已经读取txt文档的内容到变量txt中,如下
txt = \
"""abc 1,208,757,887,678,275
def 1,208,757,887,678,291
"""

# 按txt的格式将数据归纳到一个字典里
items = txt.split()
dict_ = {}
for i, item in enumerate(items):
if i % 2:
dict_[key] = int(item.replace(',', ''))
else:
key = item

print dict_

# 计算你想要的差值
value = dict_['abc'] - dict_['def']

print value

# 结果写入文件
f = open('c:/1.txt', 'w')
f.write(str(value))
f.close()

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧