split python file into different data types

Solutions on MaxInterview for split python file into different data types by the best coders in the world

showing results for - "split python file into different data types"
Frieda
04 Jan 2020
1##Write a Python program to create a file of elements of any data type (mixed data type, i.e. some  elements maybe of type int, some elements of type float and some elements of type string). Split  this file into three file containing elements of same data type (i.e. 1st file of integers only, 2nd file of float only and 3rd file of strings only). Take input from the user to create the file.  
2f = open('MixedFile.txt','w')
3while True :
4    user = input("Enter Any Data Type Element :: ")
5    if user == 'end':
6        print('!!!!!!!! EXIT !!!!!!!!!!!!')
7        break
8    else :
9        f.write(user + '\n')
10f.close()
11f = open('MixedFile.txt')
12a = []
13a = f.read().split()
14f.close()
15fs = open ('StringFile.txt','w')
16ff = open ('FloatFile.txt','w')
17fn = open ('NumberFile.txt','w')
18for i in a :
19    try:
20        int(i)
21        fn.write(i + '\n')
22    except:
23            try:
24                float(i)
25                ff.write(i + '\n')
26            except:
27                fs.write(i + '\n')
28f.close()
29fs.close()
30fn.close()
31ff.close()
32
33print("reading................")
34fs = open ('StringFile.txt','r')
35ff = open ('FloatFile.txt','r')
36fn = open ('NumberFile.txt','r')
37print(fs.read())
38print(fn.read())
39print(ff.read())
40