【问题标题】:string manipulation col_schema="col1 string,col2 int". I need to retrieve => "col1,col2"字符串操作 col_schema="col1 string,col2 int"。我需要检索 => "col1,col2"
【发布时间】:2019-09-27 01:17:20
【问题描述】:

我有一个字符串col_schema="col1 string,col2 int"
现在我必须单独检索列名。像这样 => output="col1,col2" 尝试在下面做,

    name, value = col_list.split(' ')

Traceback(最近一次调用最后一次): 文件“”,第 1 行,在 ValueError:解压的值太多

     col_list_split    = col_list.split(',')
    >>> print col_list_split
    ['col1 string', 'col2 string']
    >>> name, value = col_list_split.split(' ')

Traceback(最近一次调用最后一次): 文件“”,第 1 行,在 AttributeError: 'list' 对象没有属性 'split'

【问题讨论】:

    标签: python string


    【解决方案1】:

    用逗号分割后,可以在空格上分割每个元素

    col_list_split    = (x.split() for x in col_schema.split(','))
    

    然后你有一个列表列表,其中第一个元素是列名,你可以用逗号加入

    result = ','.join(x[0] for x in col_list_split)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 2014-09-21
      • 2019-05-22
      相关资源
      最近更新 更多