Python:(IndexError:列表索引超出范围)readFile

2012年

我在一个文本文件文件中得到了这个文本:

_2015.9.30 - 15:36:3 , 13_
_2015.9.30 - 15:36:6 , 24_
_2015.9.30 - 15:36:8 , 33_

我想要这样

_data=['2015.9.30 - 15:36:3', '2015.9.30 - 15:36:6', '2015.9.30 -15:36:8']_
_values=['13', '24', '33']_

所以我尝试了这段代码

def getData(path):
   data = []

   readFile = open(path,'r')
   sepFile = readFile.read().split('\n')
   readFile.close()

   for i in sepFile:
       myData = i.split(',')
       data.append(myData[0])

   return data



def getValues (path):
   values = []

   readFile = open(path,'r')
   sepFile = readFile.read().split('\n')
   readFile.close()

   for i in sepFile:
       myValues = i.split(',')
       values.append(myValues[1])

   return values

print getData("mytext.txt")
print getValues("mytext.txt")

第一种方法getData可以正常工作,但是第二种方法不想工作.. errormessage:

['2015.9.30 - 15:36:3 ', '2015.9.30 - 15:36:6 ', '2015.9.30 - 15:36:8'] 

Traceback (most recent call last):
File "C:\Python27\z.NEW\schrottplatz.py", line 34, in <module>
print getValues("mytext.txt")
File "C:\Python27\z.NEW\schrottplatz.py", line 29, in getValues
values.append(myValues[1])
IndexError: list index out of range
科迪嘴

file.txt

_2015.9.30 - 15:36:3 , 13_
_2015.9.30 - 15:36:6 , 24_
_2015.9.30 - 15:36:8 , 33_

代码

with open('file.txt') as f:
    data = f.read().splitlines()

_data, _values = [], []
for d in data:
    val = d.split(' , ')
    _data.append(val[0][1:])
    _values.append(val[1][:-1])

print _data
print _values
#['2015.9.30 - 15:36:3', '2015.9.30 - 15:36:6', '2015.9.30 - 15:36:8']
#['13', '24', '33']

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

python列表中的IndexError-“列表索引超出范围”

来自分类Dev

Python,列表错误:IndexError:列表索引超出范围

来自分类Dev

Python IndexError:列表分配索引超出范围

来自分类Dev

python错误:IndexError:列表索引超出范围

来自分类Dev

IndexError:列表分配索引超出范围,Python

来自分类Dev

Python 3.7.2:IndexError:列表索引超出范围

来自分类Dev

Python CSV:IndexError:列表索引超出范围

来自分类Dev

Python Web抓取“ IndexError:列表索引超出范围”

来自分类Dev

IndexError:列表索引超出范围Python参数输入

来自分类Dev

Python-IndexError:列表索引超出范围

来自分类Dev

IndexError:列表索引超出范围(Python 2.7)

来自分类Dev

Python IndexError:列表索引超出范围PushBullet

来自分类Dev

IndexError:列表分配索引超出范围-带数组的Python

来自分类Dev

python xpath IndexError:列表索引超出范围

来自分类Dev

IndexError:列表索引超出范围错误python

来自分类Dev

python 3 IndexError:列表索引超出范围

来自分类Dev

Python 3:IndexError:列表索引超出范围

来自分类Dev

IndexError:Python 3中的列表索引超出范围

来自分类Dev

IndexError:列表索引超出范围 - Python 3.5.2

来自分类Dev

IndexError:列表索引超出范围(Python 3)

来自分类Dev

IndexError:在python列表中列出索引超出范围

来自分类Dev

获取IndexError:在python中列表分配索引超出范围

来自分类Dev

Python - IndexError:列表索引超出范围 - 不工作

来自分类Dev

Python 单元测试 - IndexError:列表索引超出范围

来自分类Dev

python中的“IndexError:列表索引超出范围”错误

来自分类Dev

IndexError:列表索引超出范围

来自分类Dev

Yfinance IndexError:列表索引超出范围

来自分类Dev

IndexError:列表索引超出范围

来自分类Dev

IndexError:列表索引超出范围为何?

Related 相关文章

  1. 1

    python列表中的IndexError-“列表索引超出范围”

  2. 2

    Python,列表错误:IndexError:列表索引超出范围

  3. 3

    Python IndexError:列表分配索引超出范围

  4. 4

    python错误:IndexError:列表索引超出范围

  5. 5

    IndexError:列表分配索引超出范围,Python

  6. 6

    Python 3.7.2:IndexError:列表索引超出范围

  7. 7

    Python CSV:IndexError:列表索引超出范围

  8. 8

    Python Web抓取“ IndexError:列表索引超出范围”

  9. 9

    IndexError:列表索引超出范围Python参数输入

  10. 10

    Python-IndexError:列表索引超出范围

  11. 11

    IndexError:列表索引超出范围(Python 2.7)

  12. 12

    Python IndexError:列表索引超出范围PushBullet

  13. 13

    IndexError:列表分配索引超出范围-带数组的Python

  14. 14

    python xpath IndexError:列表索引超出范围

  15. 15

    IndexError:列表索引超出范围错误python

  16. 16

    python 3 IndexError:列表索引超出范围

  17. 17

    Python 3:IndexError:列表索引超出范围

  18. 18

    IndexError:Python 3中的列表索引超出范围

  19. 19

    IndexError:列表索引超出范围 - Python 3.5.2

  20. 20

    IndexError:列表索引超出范围(Python 3)

  21. 21

    IndexError:在python列表中列出索引超出范围

  22. 22

    获取IndexError:在python中列表分配索引超出范围

  23. 23

    Python - IndexError:列表索引超出范围 - 不工作

  24. 24

    Python 单元测试 - IndexError:列表索引超出范围

  25. 25

    python中的“IndexError:列表索引超出范围”错误

  26. 26

    IndexError:列表索引超出范围

  27. 27

    Yfinance IndexError:列表索引超出范围

  28. 28

    IndexError:列表索引超出范围

  29. 29

    IndexError:列表索引超出范围为何?

热门标签

归档