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

2024/11/20 12:28:08

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.]

Answer

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))
https://en.xdnf.cn/q/26318.html

Related Q&A

How can I get stub files for `matplotlib`, `numpy`, `scipy`, `pandas`, etc.?

I know that the stub files for built-in Python library for type checking and static analysis come with mypy or PyCharm installation. How can I get stub files for matplotlib, numpy, scipy, pandas, etc.?…

Pipfile.lock out of date

Im trying to deploy a large django project to heroku. I installed Heroku CLI, logged in, created an app and ran:git push heroku masterI have a Pipfile and requirements.txt already set up. I added a run…

Can a simple difference in Python3 variable names alter the way code runs? [duplicate]

This question already has answers here:Python attributeError on __del__(2 answers)Closed 9 years ago.This code...class Person:num_of_people = 0def __init__(self, name):self.name = namePerson.num_of_peo…

Easy way to check that a variable is defined in python? [duplicate]

This question already has answers here:How do I check if a variable exists?(15 answers)Closed 10 years ago.Is there any way to check if a variable (class member or standalone) with specified name is d…

Adding install_requires to setup.py when making a python package

To make a python package, in setup.py, I have the following: setup(name=TowelStuff,version=0.1.0,author=J. Random Hacker,author_email=[email protected],packages=[towelstuff, towelstuff.test],scripts=[b…

pyqt: how to remove a widget?

I have a QGroupBox widget with children in it that I want to remove. How do I do that? I cant find any removeWidget, removeChild, removeItem, or anything similar in the docs. I can only see how to rem…

ValueError: cannot switch from manual field specification to automatic field numbering

The class:class Book(object):def __init__(self, title, author):self.title = titleself.author = authordef get_entry(self):return "{0} by {1} on {}".format(self.title, self.author, self.press)C…

Retrieve name of column from its Index in Pandas

I have a pandas dataframe and a numpy array of values of that dataframe. I have the index of a specific column and I already have the row index of an important value. Now I need to get the column name …

Purpose of return self python

I have a problem with return selfclass Fib: def __init__(self, max):self.max = maxdef __iter__(self): self.a = 0self.b = 1return selfdef __next__(self):fib = self.aif fib > self.max:raise StopIterat…

tempfile.TemporaryDirectory context manager in Python 2.7

Is there a way to create a temporary directory in a context manager with Python 2.7?with tempfile.TemporaryDirectory() as temp_dir:# modify files in this dir# here the temporary diretory does not exis…