0
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

1
  • I suspect you want to be splitting your lines (or perhaps using the csv module, which will split it for you in a slightly more sophisticated way). Then you can reverse the list of strings before re-joining them with commas, rather than reversing a single string. Commented Sep 23, 2017 at 6:41

1 Answer 1

2

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:

  1. Split the words based on , and get list of words:

    word_list = iterms.split(',') 
    
  2. Reverse the words in the list:

    reversed_word_list = word_list[::-1]
    
  3. Join the reversed wordlist with ,

    new_line = ','.join(reversed_word_list)
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.