Python String Replace Error

2024/10/10 6:20:14

I have a python script that keeps returning the following error:

TypeError: replace() takes at least 2 arguments (1 given)

I cannot for the life of me figure out what is causing this.

Here is part of my code:

inHandler = open(inFile2, 'r')
outHandler = open(outFile2, 'w')for line in inHandler:str = str.replace("set([u'", "")str = str.replace("'", "")str = str.replace("u'", "")str = str.replace("'])", "")outHandler.write(str)inHandler.close()
outHandler.close()

Everything that is seen within double quotations needs to be replaced with nothing.

So set([u' should look like

Answer

This is what you want to do:

for line in inHandler:line = line.replace("set([u'", "")line = line.replace("'", "")line = line.replace("u'", "")line = line.replace("'])", "")outHandler.write(line)

On the documentation, wherever it says something like str.replace(old,new[,count]) the str is an example variable. In fact, str is an inbuilt function, meaning you never want to change what it means by assigning it to anything.

line = line.replace("set([u'", "")^This sets the string equal to the new, improved string.line = line.replace("set([u'", "")^ This is the string of what you want to change.
https://en.xdnf.cn/q/69925.html

Related Q&A

How to run two modules at the same time in IDLE

I am working on a super simple socket program and I have code for the client and code for the server. How do I run both these .py files at the same time to see if they work ?

Passing 2 dimensional C array to python numpy

I need some help regarding passing C array to python(numpy). I have 2d array of doubles NumRows x NumInputs, it seems that PyArray_SimpleNewFromData does not convert it right way - it is hard to see be…

Best way to implement numpy.sin(x) / x where x might contain 0

What I am doing now is:import numpy as npeps = np.finfo(float).epsdef sindiv(x):x = np.abs(x)return np.maximum(eps, np.sin(x)) / np.maximum(eps, x)But there is quite a lot of additional array operation…

Scrapy process.crawl() to export data to json

This might be a subquestion of Passing arguments to process.crawl in Scrapy python but the author marked the answer (that doesnt answer the subquestion im asking myself) as a satisfying one.Heres my pr…

Embedding Python in C: Error in linking - undefined reference to PyString_AsString

I am trying to embed a python program inside a C program. My OS is Ubuntu 14.04I try to embed python 2.7 and python 3.4 interpreter in the same C code base (as separate applications). The compilation a…

How can I add properties to a class using a decorator that takes a list of names as argument?

I would like to add many dummy-properties to a class via a decorator, like this:def addAttrs(attr_names):def deco(cls):for attr_name in attr_names:def getAttr(self):return getattr(self, "_" +…

How to reshape only last dimensions in numpy?

Suppose I have A of shape (...,96) and want to reshape it into (...,32,3) keeping both lengths and number of preceding dimensions, if ever, intact.How to do this?If I writenp.reshape(A, (-1, 32, 2))it…

Relative import of submodule

In Python, how do I perform the equivalent of the followingimport http.clientbut using a relative import:from . import http.client import .http.clientFor a package http in the current package? I want …

Python regular expression to replace everything but specific words

I am trying to do the following with a regular expression:import re x = re.compile([^(going)|^(you)]) # words to replace s = I am going home now, thank you. # string to modify print re.sub(x, _, s)T…

How do I raise a window that is minimized or covered with PyGObject?

Id been using the answer provided in the PyGTK FAQ, but that doesnt seem to work with PyGObject. For your convenience, here is a test case that works with PyGTK, and then a translated version that does…