I want to replace single quotes with double quotes in a list

前端 未结 3 443
一生所求
一生所求 2020-12-05 10:55

So I am making a program that takes a text file, breaks it into words, then writes the list to a new text file.

The issue I am having is I need the strings in the li

3条回答
  •  温柔的废话
    2020-12-05 11:31

    You cannot change how str works for list.

    How about using JSON format which use " for strings.

    >>> animals = ['dog','cat','fish']
    >>> print(str(animals))
    ['dog', 'cat', 'fish']
    
    >>> import json
    >>> print(json.dumps(animals))
    ["dog", "cat", "fish"]
    

    import json
    
    ...
    
    textfile.write(json.dumps(words))
    

提交回复
热议问题