Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 list to be with double quotes not single quotes.

For example

I get this ['dog','cat','fish'] when I want this ["dog","cat","fish"]

Here is my code

with open('input.txt') as f:     file = f.readlines() nonewline = [] for x in file:     nonewline.append(x[:-1]) words = [] for x in nonewline:     words = words + x.split() textfile = open('output.txt','w') textfile.write(str(words)) 

I am new to python and haven't found anything about this. Anyone know how to solve this?

[Edit: I forgot to mention that i was using the output in an arduino project that required the list to have double quotes.]

like image 665
ThatsOkay Avatar asked Feb 12 '17 02:02

ThatsOkay


People also ask

How do you replace single quotes with double quotes?

Use the String. replace() method to replace double with single quotes, e.g. const replaced = str. replace(/"/g, "'"); . The replace method will return a new string where all occurrences of double quotes are replaced with single quotes.

How do you replace a single quote?

Method 1 : Using the replace() method To replace a single quote from the string you will pass the two parameters. The first is the string you want to replace and the other is the string you want to place. In our case it is string. replace(” ' “,” “).

How do you change single quotes to double quotes in Excel?

As shown above, even though you want to replace a single quote at a time, you need to put double quotes between the double quotes, i.e. to replace a single quote you actually need 4 quotes in a row.


2 Answers

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)) 
like image 160
falsetru Avatar answered Sep 21 '22 17:09

falsetru


Most likely you'll want to just replace the single quotes with double quotes in your output by replacing them:

str(words).replace("'", '"') 

You could also extend Python's str type and wrap your strings with the new type changing the __repr__() method to use double quotes instead of single. It's better to be simpler and more explicit with the code above, though.

class str2(str):     def __repr__(self):         # Allow str.__repr__() to do the hard work, then         # remove the outer two characters, single quotes,         # and replace them with double quotes.         return ''.join(('"', super().__repr__()[1:-1], '"'))  >>> "apple" 'apple' >>> class str2(str): ...     def __repr__(self): ...         return ''.join(('"', super().__repr__()[1:-1], '"')) ... >>> str2("apple") "apple" >>> str2('apple') "apple" 
like image 28
Harvey Avatar answered Sep 23 '22 17:09

Harvey