python 读取文本文件内容转化为python的list

来源:互联网 发布:java源代码分享 编辑:程序博客网 时间:2024/05/16 17:52


1.目的

读取cal.txt内容,然后通过python脚本转化为list内容

2. 文件内容

 cal.txt

[plain] view plaincopy
  1. 12  
  2. 13  
  3. 14  
  4. 15  
  5. 16  

cal.py脚本内容:

[python] view plaincopy
  1. #!/usr/bin/python  
  2. #coding = UFT-8  
  3. result=[]  
  4. fd = file( "cal.txt""r" )  
  5.   
  6. for line in fd.readlines():  
  7.     result.append(list(map(int,line.split(','))))  
  8. print(result)  
  9.   
  10. #或者
  11. with open("cal.txt""r") as f:
  12.     result.append(list(map(int, f.readline().split(','))))

  13. for item in result:  
  14.     for it in item:  
  15.        print it  

执行结果内容:

[plain] view plaincopy
  1. [[12], [13], [14], [15], [16]]  
  2. 12  
  3. 13  
  4. 14  
  5. 15  
  6. 16  



0 0
原创粉丝点击