Answers for "split python file into different data types"

1

split python file into different data types

##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.  
f = open('MixedFile.txt','w')
while True :
    user = input("Enter Any Data Type Element :: ")
    if user == 'end':
        print('!!!!!!!! EXIT !!!!!!!!!!!!')
        break
    else :
        f.write(user + '\n')
f.close()
f = open('MixedFile.txt')
a = []
a = f.read().split()
f.close()
fs = open ('StringFile.txt','w')
ff = open ('FloatFile.txt','w')
fn = open ('NumberFile.txt','w')
for i in a :
    try:
        int(i)
        fn.write(i + '\n')
    except:
            try:
                float(i)
                ff.write(i + '\n')
            except:
                fs.write(i + '\n')
f.close()
fs.close()
fn.close()
ff.close()

print("reading................")
fs = open ('StringFile.txt','r')
ff = open ('FloatFile.txt','r')
fn = open ('NumberFile.txt','r')
print(fs.read())
print(fn.read())
print(ff.read())
Posted by: Guest on April-22-2021

Code answers related to "split python file into different data types"

Python Answers by Framework

Browse Popular Code Answers by Language