How to replace single quotes from a list in python

前端 未结 3 679
我寻月下人不归
我寻月下人不归 2021-01-25 10:28

I have a list:

my_list = [\'\"3\"\', \'\"45\"\',\'\"12\"\',\'\"6\"\']

This list has single and double quotes and the item value. How can I repl

相关标签:
3条回答
  • 2021-01-25 10:38

    You can use split:

    [x.split('"')[1] for x in my_list]
    

    or you can use:

    [x.strip('"') for x in my_list]
    
    0 讨论(0)
  • 2021-01-25 10:43

    As suggested by @MartijnPieters in comments, you can use replace on the strings to get the desired output.

    The change I like to suggest is that using .replace('"', '') instead of .replace('"', ' '). Otherwise the resultant strings will have a leading and trailing white space

    You can use list comprehension to deal with the list you have like this

    my_list = ['"3"', '"45"','"12"','"6"']
    
    new_list = [x.replace('"', '') for x in my_list]
    
    print(new_list) # ['3', '45', '12', '6']
    
    0 讨论(0)
  • 2021-01-25 10:49

    Your list doesn't contain any strings with single quotes. I think you are confusing the repr() representation of the strings with their values.

    When you print a Python standard library container such as a list (or a tuple, set, dictionary, etc.) then the contents of such a container are shown their repr() representation output; this is great when debugging because it makes it clear what type of objects you have. For strings, the representation uses valid Python string literal syntax; you can copy the output and paste it into another Python script or the interactive interpreter and you'll get the exact same value.

    For example, s here is a string that contains some text, some quote characters, and a newline character. When I print the string, the newline character causes an extra blank line to be printed, but when I use repr(), you get the string value in Python syntax form, where the single quotes are part of the syntax, not the value. Note that the newline character also is shown with the \n syntax, exactly the same as when I created the s string in the first place:

    >>> s = 'They heard him say "Hello world!".\n'
    >>> print(s)
    They heard him say "Hello world!".
    
    >>> print(repr(s))
    'They heard him say "Hello world!".\n'
    >>> s
    'They heard him say "Hello world!".\n'
    

    And when I echoed the s value at the end, the interactive interpreter also shows me the value using the repr() output.

    So in your list, your strings do not have the ' characters as part of the value. They are part of the string syntax. You only need to replace the " characters, they are part of the value, because they are inside the outermost '...' string literal syntax. You could use str.replace('"', '') to remove them:

    [value.replace('"', '') for value in my_list]
    

    or, you could use the str.strip() method to only remove quotes that are at the start or end of the value:

    [value.strip('"') for value in my_list]
    

    Both work just fine for your sample list:

    >>> my_list = ['"3"', '"45"','"12"','"6"']
    >>> [value.replace('"', '') for value in my_list]
    ['3', '45', '12', '6']
    >>> [value.strip('"') for value in my_list]
    ['3', '45', '12', '6']
    

    Again, the ' characters are not part of the value:

    >>> first = my_list[0].strip('"')
    >>> first         # echo, uses repr()
    '3'
    >>> print(first)  # printing, the actual value written out
    3
    >>> len(first)    # there is just a single character in the string
    1
    

    However, I have seen that you are reading your data from a tab-separated file that you hand-parse. You can avoid having to deal with the " quotes altogether if you instead used the csv.reader() object, configured to handle tabs as the delimiter. That class automatically will handle quoted columns:

    import csv
    
    with open(inputfile, 'r', newline='') as datafile:
        reader = csv.reader(datafile, delimiter='\t')
        for row in reader:
            # row is a list with strings, *but no quotes*
            # e.g. ['3', '45', '12', '6']
    

    Demo showing how csv.reader() handles quotes:

    >>> import csv
    >>> lines = '''\
    ... "3"\t"45"\t"12"\t"6"
    ... "42"\t"81"\t"99"\t"11"
    ... '''.splitlines()
    >>> reader = csv.reader(lines, delimiter='\t')
    >>> for row in reader:
    ...     print(row)
    ...
    ['3', '45', '12', '6']
    ['42', '81', '99', '11']
    
    0 讨论(0)
提交回复
热议问题