Reputation: 1
f = open (FilePath, "r")
#print f
with open(FilePath, "r") as f:
lines = f.readlines()
#print lines
for iterms in lines:
new_file = iterms[::-1]
print new_file
it gives me a result like this: 7340.12,8796.4871825,0529.710635,751803.0,fit.69-81-63-40tuo
original list is like this: out04-32-45-95.tif,0.330693,536043.5237,5281852.0362,20.2260
it is supposed to be like this: 20.2260, ...........out04-32-45-95.tif
Upvotes: 0
Views: 41
Reputation: 48092
You should be using your for
loop like:
for iterms in lines:
new_file = ','.join(iterms.split(',')[::-1])
print new_file
Explanation:
In your current code, the line iterms[::-1]
reverses the entire string present in your line. But you want to only reverse the words separated by ,
.
Hence, you need to follow below steps:
Split the words based on ,
and get list of words:
word_list = iterms.split(',')
Reverse the words in the list:
reversed_word_list = word_list[::-1]
Join the reversed wordlist with ,
new_line = ','.join(reversed_word_list)
Upvotes: 2