using python extract string from a list of string

后端 未结 1 411
你的背包
你的背包 2021-01-07 15:24

when i was reading CSV header using following code

def  read_header(gram):
    CSV_tag_read = csv.reader(open(\"Dataset/MALE_Training/\"+str(gram)+\"/\"+str(         


        
相关标签:
1条回答
  • 2021-01-07 16:14

    Using ast.literal_eval:

    >>> import ast
    >>> a = "('TO', 'RP', 'TO')"
    >>> b = ast.literal_eval(a)
    >>> b
    ('TO', 'RP', 'TO')
    >>> b[0]
    'TO'
    

    Or

    >>> [x.strip("' ") for x in a.strip('()').split(',')]
    ['TO', 'RP', 'TO']
    

    UPDATED ANSWER that corresponding to updated question:

    with open('/path/to/csv-file.csv') as f:
        reader = csv.reader(f, delimiter='"')
        header = [column.replace("'", "") for column in header]
    
    0 讨论(0)
提交回复
热议问题