compare string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ndoe
    New Member
    • Sep 2007
    • 12

    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
    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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You could do something like this:
    [code=Python]>>> line1 = '12345648737276 488'
    >>> line2 = 'abcdef'
    >>> import string
    >>> numbers = set('1234567890 ')
    >>> alpha = set(string.asci i_letters)
    >>> set(line1).issu bset(numbers)
    True
    >>> set(line2).issu bset(alpha)
    True
    >>> set(line2).issu bset(numbers)
    False
    >>> [/code]

    Comment

    • ndoe
      New Member
      • Sep 2007
      • 12

      #3
      hmmm,not like that,i mean how to write to file and we read that string from file to thanks for your help

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        By what criteria are you reordering the data to write to another file?

        Comment

        • nishi2rock
          New Member
          • Nov 2008
          • 3

          #5
          If your problem is about reading from and into files...

          FileObj = open(Filename,m ode) 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.isal pha() = True
          someString.isal num() = True
          someString.isdi git() = Flase

          someString = '123456'

          someString.isal pha() = False
          someString.isal num() = True
          someString.isdi git() = True

          may be if you explain your sorting criteria it would be more explanatory

          Comment

          Working...