how to compare string in file
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

Here is a way

def createData2():
    L = [line.strip()  for line in open("data.txt")]
    L = [line for line in L if line]
    data2 = open("data2.txt", "w")
    data2.write(" ".join(L))
    data2.close()

if __name__ == "__main__":
    createData2()

Without using list comprehension:

f = open( "data.txt" )
lines = f.readlines()
f.close()
f = open( "data2.txt", 'w' )

for line in lines:
    line = line.strip()
    if line:
        f.write( "%s " % line )

f.write( "\n" )
f.close()

hmmm,not like that i mean can we knows that string is made from digit or alphabet,and 1 question again can when we read line in file we can count the line,i mean variable count the line when read 1st line variable = 1 line read line 2nd variable = 2,and so on thanks

Here's a possibility:

import string

numbers = set('1234567890')
alpha = set(string.ascii_letters)
mixed = numbers.add(alpha)

f = open(file_name)
for i, line in enumerate(f):
    # i is the line number beginning with 0
    s1 = set(line.strip())
    if s1.issubset(numbers):
        # do something
    elif s1.issubset(alpha):
        # do something
    elif s1.issubset(mixed):
        # do something
    else:
        # do something else

f.close()

My previous post has an error regarding the assignment to variable mixed. It should be:

mixed = set('1234567890')
mixed.add(alpha)

OR

mixed = set('1234567890'+string.ascii_letters)

My previous post has an error regarding the assignment to variable mixed. It should be:

mixed = set('1234567890')
mixed.add(alpha)

OR

mixed = set('1234567890'+string.ascii_letters)

i had been try for yours code for reading writing file and compare string from file. but its failed when i modify here the code

#Boa:Frame:Frame1

import wx
import string

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, 
] = [wx.NewId() for _init_ctrls in range(2)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(380, 278), size=wx.Size(142, 121),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(134, 87))

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='button1',
              name='button1', parent=self, pos=wx.Point(0, 0), size=wx.Size(134,
              87), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_FRAME1BUTTON1)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def OnButton1Button(self, event): 
        numbers = set('1234567890')
        alpha = set(string.ascii_letters)
        mixed = set('1234567890'+string.ascii_letters)
        f = open("data.txt")
        for i, line in enumerate(f):
            # i is the line number beginning with 0
            s1 = set(line.strip())
            if s1.issubset(numbers):
                f = open( "data2.txt", 'w' )
                f.write( "%s " % w )
            #elif s1.issubset(alpha):
            #    f = open( "data2.txt", 'w' )
            #    f.write( "%s " % line )
            #elif s1.issubset(mixed):
            #    f = open( "data2.txt", 'w' )
            #    f.write( "%s " % line )
            #else:
            #    f = open( "data2.txt", 'w' )
            #    f.write( "%s " % line )
        f.write( "\n" )
        f.close()
               
               
               
               
               
'''lines = f.readlines()
f.close()
f = open( "data2.txt", 'w' )

for line in lines:
    line = line.strip()
    if line:
        f.write( "%s " % line )

f.write( "\n" )
f.close()'''

i want to write the string from the data.txt

Route
123456
Route
123456
Route
123456
Route
123456
Route
123456
Route
123456

but in data2.txt just

123456

i want to write all string from data.txt like this

route
....
....
route #6 times 
123456
...
....
123456 #6 times

can you repair the code thanks

import string

numbers = set('1234567890')
alpha = set(string.ascii_letters)
mixed = set('1234567890')
mixed.add(alpha)

file_name = 'data5.txt'

f = open(file_name)
numList = []
alphaList = []
mixedList = []
for i, line in enumerate(f):
    # i is the line number beginning with 0
    s1 = set(line.strip())
    if s1.issubset(numbers):
        numList.append(line.strip())
    elif s1.issubset(alpha):
        alphaList.append(line.strip())
    elif s1.issubset(mixed):
        mixedList.append(line.strip())

f.close()
f = open('data6.txt', 'w')
f.write('\n'.join(['\n'.join([item for item in eachList]) for eachList in [alphaList, numList, mixedList]]))
f.close()

Output:

Route
Route
Route
Route
Route
Route
123456
123456
123456
123456
123456
123456

May 22 00:46:26 dex0010 syslogd 1.4.1: restart
how can i split thiz line nd compare the date nd time with present days

This article has been dead for over six months. Start a new discussion instead.