Home > Software engineering >  Python print incomplete data
Python print incomplete data

Time:11-10

I have the following data in raw_data.txt

id=001;name=Johan Strauss;address=58 Jalan Jelutong, 11600 Pulau Pinang, Malaysia;gender=male
id=002;name=Jennifer Teng;address=Q-2-4,Desa Indah, Lorong Minyak, 13000 Kedah, Malaysia;gender=female

I ran the following code:

f = open('raw_data.txt', 'r')
for x in f:
x = x.replace("id","Student",)
x = x.replace("name","Name")
x = x.replace("address","Home Number")
x = x.replace("gender","Gender")
x = x.replace(";","\n")
print (x)

g = open('formatted.txt','a')
g.write(x)

This is the output I got in formatted.txt

Student=002
Name=Jennifer Teng
Home Number=Q-2-4,Desa Indah, Lorong Minyak, 13000 Kedah, Malaysia
Gender=female

I lost student 001 data. What is wrong with my code?

CodePudding user response:

Problem: you are writing to file in the end only once but processing more student data in the for loop before. The x variable only stores the final value and writes the student 002 data only.

with open('data.txt', 'r') as f: # reading all data line by line from the raw_data.txt
    data = f.readlines()
    
with open('formatted.txt', 'w') as f: # writing data to new file student by student
    for student in data:
        student = student.strip().replace("id","Student").replace("name","Name").replace("address","Home Number").replace("gender","Gender").replace(";","\n")
        f.write(student)
        print(student)

File output screenshot

CodePudding user response:

Remember to close the file that you opened. Your code has an indentation problem that the for loop iterates line by line. The corrected code can be corrected as follows:

f = open('raw_data.txt', 'r')
y=''
for x in f:
    x = x.replace("id","Student")
    x = x.replace("name","Name")
    x = x.replace("address","Home Number")
    x = x.replace("gender","Gender")
    x = x.replace(";","\n")
    y=y x #Used to store the newly formated text of each line
print(y)
f.close()
g = open('formatted.txt','a')
g.write(y) #Write the updated information to the file
g.close()
  • Related