Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Split()函数无明显原因地拆分_Python_Python 3.x_Split - Fatal编程技术网

Python Split()函数无明显原因地拆分

Python Split()函数无明显原因地拆分,python,python-3.x,split,Python,Python 3.x,Split,我对split函数有一个问题,由于某种原因,它在某个不是分隔符的地方分裂,。我正在读一个逗号分隔的文件,里面有基因和一些属性。但是当我从文件中读到这一行时 G234064,必需,GTP/GDP交换因子GEFs,翻译复合物,PS00824,1,细胞组织蛋白定位于相应的细胞器,细胞质 但不是在列表中分成9个不同的项目,而是分成以下10个: ['G240504', 'Non-Essential', '?', '?', 'Sensitivity to aminoacid analogs

我对split函数有一个问题,由于某种原因,它在某个不是分隔符的地方分裂,。我正在读一个逗号分隔的文件,里面有基因和一些属性。但是当我从文件中读到这一行时

G234064,必需,GTP/GDP交换因子GEFs,翻译复合物,PS00824,1,细胞组织蛋白定位于相应的细胞器,细胞质


但不是在列表中分成9个不同的项目,而是分成以下10个:

['G240504',
  'Non-Essential',
  '?',
  '?',
  'Sensitivity to aminoacid analogs and other drugs',
  'PS00868',
  '12',
  'CELLULAR ORGANIZATION (proteins are localized to the corresponding '
  'organelle)',
  'cytoplasm.']
我找不到有类似问题的人,这是我使用的代码

def main():
    file = open("Genes_relation.data.txt")
    readfile = file.readlines()
    genlist = []
    for list in readfile:
        genlist.append(list.rstrip("\n").split(","))
我希望输出是

['G240504',
  'Non-Essential',
  '?',
  '?',
  'Sensitivity to aminoacid analogs and other drugs',
  'PS00868',
  '12',
  'CELLULAR ORGANIZATION (proteins are localized to the corresponding 
   organelle)',
  'cytoplasm.']

有人知道发生了什么吗?

我试图重现你的错误,但我不能。您必须检查文件中的编码,可能有一些意外的逗号,或者编码错误。有时在生物数据中存在不同的编码,这会导致不同的意外行为。我不能为你做更多的事了

>>> x = "G234064,Essential,GTP/GDP-exchange factors (GEFs),Translation complexes,?,PS00824,1,CELLULAR ORGANIZATION (proteins are localized to the corresponding organelle),cytoplasm"
>>> x.split(',')
['G234064', 
'Essential', 
'GTP/GDP-exchange factors (GEFs)', 
'Translation complexes', 
'?', 
'PS00824', 
'1', 
'CELLULAR ORGANIZATION (proteins are localized to the corresponding organelle)',
 'cytoplasm']

问题正文中的行与代码中的行不同。但不是在列表中拆分为9个不同的项目,而是拆分为这10个项目-那里只有9个项目,对应的行末尾没有逗号“您可以将实际输入也放在这里,而不仅仅是预期输出吗?”。这有助于更好地理解上下文。请仔细查看您的输出。它有9个元素,而不是10个。没有,在“对应”之后。例如,此['foo''bar']与['foobar']相同。我不知道你是怎么得到这个结果的,但它有9个元素,不是10个。格式只是误导。最好的验证方法是在分割后打印长度。如何显示输出?无论是写入文件还是打印到终端,都不会将字符串显示为两个将作为文本连接的字符串。