Windows到Linux脚本问题:“ IndexError:列表索引超出范围” - python

我有一个脚本-在Windows中可以完美运行,但是当我尝试在Ubuntu中运行它时,会吐出错误消息:

  IndexError:列表索引超出范围。

这是一个非常简单的脚本:它导入一个CSV文件,读取行,将每行中的第一项打印到列表中,使用set()删除重复项,然后将此新列表写入文件中。

import csv, glob

for x in glob.glob("*raw_vcf.csv"):
   csv_f = open(x, "r")

data = [c for c in csv.reader(csv_f)]
frags_unique = []

def frag_list(vcf_data, uniquefrags):
    """ 
    User input: an imported .vcf file (='vcf_import'); an empty list
    (= 'uniquefrags').
    'frag_list' takes 'vcf_import', reads each row/list, taking the first item
    and attaching only unique values to 'uniquefrags', using the set() function.
    First row (header row) in 'vcf_data' is deleted; not needed.
    """
    del vcf_data[0]
    list_1 = []
    for row in vcf_data:
        list_1.append(row[0])
    for item in list(set(list_1)):
        uniquefrags.append(item)

frag_list(data, frags_unique)

out = open("output_unique_frags.txt","w")
for frags in frags_unique:
    out.write(frags+"\n")
out.close()

具体来说,该错误发生在模块中:

Traceback (most recent call last):
  File "PRIME_unique_frags.py", line 50, in <module>
    frag_list(data, frags_unique)
  File "PRIME_unique_frags.py", line 46, in frag_list
    list_1.append(row[0])
IndexError: list index out of range

但是,老实说,鉴于它可以在我的Windows操作系统上运行,因此我看不出有什么问题。尝试用不同的方式重写它,但是没有运气。

一些样本输入数据(“ * _raw_vcf.csv”):

A,B,C,D,E
1,2,3,4,5
1,5,4,3,2
2,3,4,5,6
2,3,4,7,8
3,4,5,6,7

理论上(在Windows中,确实如此)会产生一个文件(“ output_unique_frags.txt”; A列中的唯一值):

1
2
3

参考方案

追溯说row没有元素[0],因此它是一个空列表。这表明在Ubuntu系统上,阅读器正在为每行返回一个空列表。

看看csv docs;您可以在设置阅读器时指定一种方言。我想说的是,Ubuntu系统上的读者正在寻找与文件中的分隔符不同的分隔符。

顺便说一句:上面的缩进代码正确吗?如果有的话,这里发生了一些奇怪的事情,例如:

for x in glob.glob("*raw_vcf.csv"):
   csv_f = open(x, "r")

如果有多个.csv文件,则只会得到最后一个。

“ python setup.py egg_info”失败,错误代码为1。如何解决此问题 - python

我该如何解决。我找不到任何带有“ Temp \ pip-install-7utykvpt \ polyglot”的目录C:\Windows\system32>pip install polyglot Collecting polyglot Using cached https://files.pythonhosted.org/packages/e7/9…

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

python- sqlite3.OperationalError:“ <”附近:语法错误 - python

我正在使用python 3.6。当我尝试实现此功能时,在以下行:cursor = conn.execute(cmd)标题出现错误,有人可以帮我吗?万分感谢。编辑:我已经找到了解决方案,只需将str(id)编辑为str(Id)def getProfile(id): conn=sqlite3.connect("FaceBase.db") cm…

Python-crontab模块 - python

我正在尝试在Linux OS(CentOS 7)上使用Python-crontab模块我的配置文件如下:{ "ossConfigurationData": { "work1": [ { "cronInterval": "0 0 0 1 1 ?", "attribute&…

Python:检查是否存在维基百科文章 - python

我试图弄清楚如何检查Wikipedia文章是否存在。例如,https://en.wikipedia.org/wiki/Food 存在,但是https://en.wikipedia.org/wiki/Fod 不会,页面只是说:“维基百科没有此名称的文章。”谢谢! 参考方案 >>> import urllib >>> prin…