By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,767 Members | 1,334 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

compare string

P: 12
how to compare string in file i mean compare that string content digit or alphabet,and 1 question more can we count that line if first line variable = 1,if read second line variable = 2 and so on thanks
e.g

i have file with name data.txt with file like this

123456




abcdef
456897
asdffg


789654

gfdsah


the question how to compare that file and i now that string with content alphabet or number
and i want make make new file data2.txt from data.txt like this


123456 456897 789654 123456 abcdef 123456 asdffg gfdsah
Nov 6 '08 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You could do something like this:
Expand|Select|Wrap|Line Numbers
  1. >>> line1 = '12345648737276488'
  2. >>> line2 = 'abcdef'
  3. >>> import string
  4. >>> numbers = set('1234567890')
  5. >>> alpha = set(string.ascii_letters)
  6. >>> set(line1).issubset(numbers)
  7. True
  8. >>> set(line2).issubset(alpha)
  9. True
  10. >>> set(line2).issubset(numbers)
  11. False
  12. >>> 
Nov 6 '08 #2

P: 12
hmmm,not like that,i mean how to write to file and we read that string from file to thanks for your help
Nov 8 '08 #3

bvdet
Expert Mod 2.5K+
P: 2,851
By what criteria are you reordering the data to write to another file?
Nov 8 '08 #4

P: 3
If your problem is about reading from and into files...

FileObj = open(Filename,mode) eg:f = open("tmp.txt","w")

read and readlines can be used to read data from file
eg: FileData = f.read() reads all the data of file into FileData

write and writelines can be used to write

coming to your string manipulations

someString = 'A string'

someString.isalpha() = True
someString.isalnum() = True
someString.isdigit() = Flase

someString = '123456'

someString.isalpha() = False
someString.isalnum() = True
someString.isdigit() = True

may be if you explain your sorting criteria it would be more explanatory
Nov 8 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.