Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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 使用多个分隔符拆分字符串_Python_List - Fatal编程技术网

Python 使用多个分隔符拆分字符串

Python 使用多个分隔符拆分字符串,python,list,Python,List,我正在从一个包含以下内容的文件中读取 87965164,Paris,Yu,6/27/1997 87965219,Heath,Moss,10/13/1996 87965187,Cale,Blankenship,10/22/1995 87965220,Terrence,Watkins,12/7/1996 87965172,Ansley,Padilla,3/30/1997 我需要在“,”和“/”处拆分行,并删除 结尾处的“\n” 我希望我的输出在放入列表时如下所示: [['87965164', 'P

我正在从一个包含以下内容的文件中读取

87965164,Paris,Yu,6/27/1997
87965219,Heath,Moss,10/13/1996
87965187,Cale,Blankenship,10/22/1995
87965220,Terrence,Watkins,12/7/1996
87965172,Ansley,Padilla,3/30/1997
我需要在“,”和“/”处拆分行,并删除 结尾处的“\n”

我希望我的输出在放入列表时如下所示:

[['87965164', 'Paris', 'Yu', 6, 27, 1997], ['87965219', 'Heath', 'Moss', 10, 13, 1996], ['87965187', 'Cale', 'Blankenship', 10, 22, 1995], ['87965220', 'Terrence', 'Watkins', 12, 7, 1996], ['87965172', 'Ansley', 'Padilla', 3, 30, 1997]]

如果您有一个特别大的文件,可以将其包装到生成器中:

import re
def splitter(fl):
   for line in fl:
     # By using a generator, you are only accessing one line of the file at a time.
     yield re.split('[,/]',line.strip())

如果您有一个特别大的文件,可以将其包装到生成器中:

import re
def splitter(fl):
   for line in fl:
     # By using a generator, you are only accessing one line of the file at a time.
     yield re.split('[,/]',line.strip())
对于每一行:

parts = line.split(',')
parts[-1:] = map(int, parts[-1].split('/'))
这将正确处理非日期部分中有任何斜杠的输入,并同时轻松处理到整数的转换。

对于每一行:

parts = line.split(',')
parts[-1:] = map(int, parts[-1].split('/'))
这将正确处理非日期部分中有任何斜杠的输入,并同时轻松处理到整数的转换。

比正则表达式更简单:

[line.replace('/', ',').split(',') for line in text.split('\n')]
之后可以将数字转换为
int
s

然而,我相信你在寻找错误的方法。正确的方法是用逗号分隔,然后对特殊字段进行专门处理

from datetime import datetime
from collections import namedtuple

Person = namedtuple('Row', ['idn', 'first', 'last', 'birth'])

def make_person(idn, first, last, birth):
    return Person(idn, first, last,
                  datetime.strptime(birth, "%m/%d/%Y"))

records = [make_person(*line.split(',')) for line in text.split('\n')]
比正则表达式更简单:

[line.replace('/', ',').split(',') for line in text.split('\n')]
之后可以将数字转换为
int
s

然而,我相信你在寻找错误的方法。正确的方法是用逗号分隔,然后对特殊字段进行专门处理

from datetime import datetime
from collections import namedtuple

Person = namedtuple('Row', ['idn', 'first', 'last', 'birth'])

def make_person(idn, first, last, birth):
    return Person(idn, first, last,
                  datetime.strptime(birth, "%m/%d/%Y"))

records = [make_person(*line.split(',')) for line in text.split('\n')]

我建议使用字典或创建类,而不是将异构数据存储在同构数据类型中

使用字典:

results = {}
with open('in.txt') as f:
    for line in f:
        id, first, last, day = line.split(',')
        month, day, year = map(int, day.split('/'))
        results[id] = {'id':id, 'first':first, 'last':last,
                       'month':month, 'day':day, 'year':year}
在课堂上:

class Person:
    def __init__(self, id, first, last, day):
        self.id = id
        self.first = first
        self.last = last
        self.month, self.day, self.year = map(int, day.split('/'))

results = {}
with open('in.txt') as f:
    for line in f:
        id, first, last, day = line.split(',')
        results[id] = Person(id, first, last, day)

请注意,在每种情况下,我都将每个人的信息作为条目存储在字典中,并带有一个看起来像他们的ID号的键。

我建议使用字典或创建一个类,而不是将异构数据存储在同构数据类型中

使用字典:

results = {}
with open('in.txt') as f:
    for line in f:
        id, first, last, day = line.split(',')
        month, day, year = map(int, day.split('/'))
        results[id] = {'id':id, 'first':first, 'last':last,
                       'month':month, 'day':day, 'year':year}
在课堂上:

class Person:
    def __init__(self, id, first, last, day):
        self.id = id
        self.first = first
        self.last = last
        self.month, self.day, self.year = map(int, day.split('/'))

results = {}
with open('in.txt') as f:
    for line in f:
        id, first, last, day = line.split(',')
        results[id] = Person(id, first, last, day)

请注意,在每种情况下,我都将每个人的信息作为条目存储在字典中,并带有一个看起来像他们的ID号的键。

你有没有尝试过任何东西?你有没有尝试过任何东西?我相信这正是
namedtuple
创建的东西for@Elazar-
namedtuple
tuple
s添加对象表示法,但是
tuple
仍然是一种同质的数据类型(与
list
s不同,它是不可变的)。我不同意。符号是重要的部分。不变性是另一个特点。而且,您可以对字段进行迭代,这使得以不同格式打印更容易。一般来说,“同构”是一个误称,因为python是动态类型的——所有容器都是“同构的”(包括类)。元组主要用于异构用途,而列表(因为它是可扩展的)主要是“真正”异构的。@Elazar-对象也提供了这种表示法,以及可变性(有时人们会更改名称)和任何其他您喜欢的行为,如用户友好的字符串表示法。Python可能不关心你有没有,例如,<代码>列表>代码>或<代码> tuple < /Cord>这是所有的整数,除了中间的一个字符串,但我不想把它当作开发人员来处理。code>namedtuple是一种方法,但在这种情况下,它在客观上并不比普通的用户定义类更好。我相信这正是
namedtuple
创建的原因for@Elazar-
namedtuple
tuple
s添加对象表示法,但是
tuple
仍然是一种同质的数据类型(与
list
s不同,它是不可变的)。我不同意。符号是重要的部分。不变性是另一个特点。而且,您可以对字段进行迭代,这使得以不同格式打印更容易。一般来说,“同构”是一个误称,因为python是动态类型的——所有容器都是“同构的”(包括类)。元组主要用于异构用途,而列表(因为它是可扩展的)主要是“真正”异构的。@Elazar-对象也提供了这种表示法,以及可变性(有时人们会更改名称)和任何其他您喜欢的行为,如用户友好的字符串表示法。Python可能不关心你有没有,例如,<代码>列表>代码>或<代码> tuple < /Cord>这是所有的整数,除了中间的一个字符串,但我不想把它当作开发人员来处理。code>namedtuple是其中一种方法,但在这种情况下,它在客观上绝不比普通的用户定义类更好。