问题
Its late and I have been trying to work on a simple script to rename point cloud data to a working format. I dont know what im doing wrong as the code at the bottom works fine. Why doesnt the code in the for loop work? It is adding it to the list but its just not getting formatted by the replace function. Sorry I know this isnt a debugger but I am really stuck on this and it would probably take 2 seconds for someone else to see the problem.
# Opening and Loading the text file then sticking its lines into a list []
filename = "/Users/sacredgeometry/Desktop/data.txt"
text = open(filename, 'r')
lines = text.readlines()
linesNew = []
temp = None
# This bloody for loop is the problem
for i in lines:
temp = str(i)
temp.replace(' ', ', ',2)
linesNew.append(temp)
# DEBUGGING THE CODE
print(linesNew[0])
print(linesNew[1])
# Another test to check that the replace works ... It does!
test2 = linesNew[0].replace(' ', ', ',2)
test2 = test2.replace('\t', ', ')
print('Proof of Concept: ' + '\n' + test2)
text.close()
回答1:
Your not assigning the return value of replace()
to anything. Also, readlines
and str(i)
are unnecessary.
Try this:
filename = "/Users/sacredgeometry/Desktop/data.txt"
text = open(filename, 'r')
linesNew = []
for line in text:
# i is already a string, no need to str it
# temp = str(i)
# also, just append the result of the replace to linesNew:
linesNew.append(line.replace(' ', ', ', 2))
# DEBUGGING THE CODE
print(linesNew[0])
print(linesNew[1])
# Another test to check that the replace works ... It does!
test2 = linesNew[0].replace(' ', ', ',2)
test2 = test2.replace('\t', ', ')
print('Proof of Concept: ' + '\n' + test2)
text.close()
回答2:
Strings are immutable. replace
returns a new string, which is what you have to insert into the linesNew
list.
# This bloody for loop is the problem
for i in lines:
temp = str(i)
temp2 = temp.replace(' ', ', ',2)
linesNew.append(temp2)
回答3:
I had a similar problem and came up with the code below to help solve it. My specific issue was that I need to swap out certain parts of a string with the corresponding label. I also wanted something that would be reusable in different places within my application.
With the code below, I'm able to do the following:
>>> string = "Let's take a trip to Paris next January"
>>> lod = [{'city':'Paris'}, {'month':'January'}]
>>> processed = TextLabeler(string, lod)
>>> processed.text
>>> Let's take a trip to [[ city ]] next [[ month ]]
Here is all of the code:
class TextLabeler():
def __init__(self, text, lod):
self.text = text
self.iterate(lod)
def replace_kv(self, _dict):
"""Replace any occurrence of a value with the key"""
for key, value in _dict.iteritems():
label = """[[ {0} ]]""".format(key)
self.text = self.text.replace(value, label)
return self.text
def iterate(self, lod):
"""Iterate over each dict object in a given list of dicts, `lod` """
for _dict in lod:
self.text = self.replace_kv(_dict)
return self.text
来源:https://stackoverflow.com/questions/7814997/running-replace-method-in-a-for-loop