Split string using a newline delimiter with Python

后端 未结 5 1081
故里飘歌
故里飘歌 2020-11-27 18:01

I need to delimit the string which has new line in it. How would I achieve it? Please refer below code.

Input:

data = \"\"\"a,b,c
d,e,f
g,h,i
j,k,l\"         


        
相关标签:
5条回答
  • 2020-11-27 18:07

    str.splitlines method should give you exactly that.

    >>> data = """a,b,c
    ... d,e,f
    ... g,h,i
    ... j,k,l"""
    >>> data.splitlines()
    ['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
    
    0 讨论(0)
  • 2020-11-27 18:18

    Here you go:

    >>> data = """a,b,c
    d,e,f
    g,h,i
    j,k,l"""
    >>> data.split()  # split automatically splits through \n and space
    ['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
    >>> 
    
    0 讨论(0)
  • 2020-11-27 18:25
    data = """a,b,c
    d,e,f
    g,h,i
    j,k,l"""
    
    print(data.split())       # ['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
    

    str.split, by default, splits by all the whitespace characters. If the actual string has any other whitespace characters, you might want to use

    print(data.split("\n"))   # ['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
    

    Or as @Ashwini Chaudhary suggested in the comments, you can use

    print(data.splitlines())
    
    0 讨论(0)
  • 2020-11-27 18:26

    If you want to split only by newlines, you can use str.splitlines():

    Example:

    >>> data = """a,b,c
    ... d,e,f
    ... g,h,i
    ... j,k,l"""
    >>> data
    'a,b,c\nd,e,f\ng,h,i\nj,k,l'
    >>> data.splitlines()
    ['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
    

    With str.split() your case also works:

    >>> data = """a,b,c
    ... d,e,f
    ... g,h,i
    ... j,k,l"""
    >>> data
    'a,b,c\nd,e,f\ng,h,i\nj,k,l'
    >>> data.split()
    ['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
    

    However if you have spaces (or tabs) it will fail:

    >>> data = """
    ... a, eqw, qwe
    ... v, ewr, err
    ... """
    >>> data
    '\na, eqw, qwe\nv, ewr, err\n'
    >>> data.split()
    ['a,', 'eqw,', 'qwe', 'v,', 'ewr,', 'err']
    
    0 讨论(0)
  • 2020-11-27 18:29

    There is a method specifically for this purpose:

    data.splitlines()
    ['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
    
    0 讨论(0)
提交回复
热议问题