I got a problem, it’s about converting from List to String to edit it
I have file text content these name
Roby
Bucky
johan
Then I want to add beside each one OK:1
so i do this code
Names='a.txt' # here is the names
O=open(Names,'r') # we open the file content the names
iamList=O.readlines() # read the name it would be List !
imSting=str(iamList) # convert it to string
Edit=imSting.replace(r"\n","Ok:1") # Editing by adding
noname=Edit
noname=noname.split()
for PT in noname:
PT.strip('[')
print PT
and i got this result
['RobyOk:1',
'BuckyOk:1',
'johan']
i want to delete these thing [ , '
i tried strip and replace('[','')
any ideas ?
You don’t need to convert it to a string to add
Ok:1at the end of each element.You could do it with a list comprehension like this:
Alternatively, to be less fancy, you could do this:
Note,
rstripstrips all whitespace, such as newlines, off of the end of the string, which I assume is what you want.