472,127 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

compare string

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

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





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
4 2897
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
By what criteria are you reordering the data to write to another file?
Nov 8 '08 #4
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.

Similar topics

3 posts views Thread by Drew | last post: by
19 posts views Thread by David zhu | last post: by
reply views Thread by William Stacey [MVP] | last post: by
4 posts views Thread by Gaby | last post: by
2 posts views Thread by Locia | last post: by
6 posts views Thread by Maileen | last post: by
4 posts views Thread by Lamis | last post: by
2 posts views Thread by Peter Proost | last post: by
11 posts views Thread by Tony | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.