Python Forum
How to compare string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to compare string (/thread-15444.html)



How to compare string - Rehan11 - Jan-17-2019

I need help to compare string

Let's say I am getting string continousely from input
input message may be following

string = ";Day1=13/10/18;Day2=14= Shift;Time11=15:17;PRICE=10"

strong = ";Day1=13/10/18;Day2=14=A;Time11=15:17;PRICE=10" #no change in strig
string = ";Day1=14/10/18;Day2=14=A;Time11=15:17;PRICE=10" #only date change
string ";Day1=13/10/18;Day2=14=B;Time11=15:17;PRICE=10" #section change
string = ";Day1=13/10/18;Day2=14=A;Time11=15:17;PRICE=12" #price change

if any change happen in input like date change then print date change, if section change then print section change if price change print price change

How to make script


RE: How to compare string - ichabod801 - Jan-17-2019

Use the split method with the semi-colon to separate the parts. Compare each part to the previous corresponding part (zip and a for loop would make this really easy). If they are different print. Store the current parts as the new previous parts and repeat.


RE: How to compare string - metulburr - Jan-17-2019

The difflib module can be useful for various tasks comparing two strings or files. It is very similar to git/mercurual.
https://pymotw.com/3/difflib/


RE: How to compare string - Rehan11 - Jan-19-2019

(Jan-17-2019, 06:08 PM)ichabod801 Wrote: ";Day1=13/10/18;Day2=14= Shift;Time11=15:17;PRICE=10"
done here
# Input string.
Input_string = ";Day1=13/10/18;Day2=14= Shift;Time11=15:17;PRICE=10"

# Separate on comma.
Separate_comma = Input_string.split(";")

# Loop and print each city name.
for string in Separate_comma:
    print(string)
output
Day1=13/10/18
Day2=14= Shift
Time11=15:17
PRICE=10
How to compare present and previous date ?
How to compare p and previous time ?
How to compare present and previous price ?