user7577503
user7577503

Reputation:

Python reading and writing to a .txt file

I need to read the names from the babynames2014.txt file and then create two new files, separating the boys and girls names. The resulting files should be called boynames2014.txt and girlnames.txt. The babynames2014.txt files looks like this:

1 Noah Emma

2 Liam Olivia

3 Mason Sophia

4 Jacob Isabella

and continues until it reaches 100 boy and girls names. The code I have written so far creates both of the new text files but the boynames2014 contains nothing and the girlnames2014 contains only the name Noah with the number 1 before it like this: 1Noah.

I think that I will need to use readline() and line.split() somewhere, I'm just not sure where and how to use them correctly. I also need to use a try/except block to handle the exception in case the babynames2014.txt file is not found.

    infile = open("babynames2014.txt", "r")    
    outfile = open("boynames2014.txt", "w")
    outfile = open("girlnames2014.txt", "w")


    line = infile.readline()
    datafield = line.split()
    boyname2014 = datafield[0]
    girlname2014 = datafield[1]
    outfile.write(boyname2014)
    outfile.write(girlname2014)


    infile.close()
    outfile.close()

I have only studied Python for 2-3 months and really appreciate any advice to help me learn more!

Upvotes: 1

Views: 866

Answers (7)

Nishanth Matha
Nishanth Matha

Reputation: 6081

Want to consider a regex solution?

with open("babynames2014.txt", "r") as f1,open("boynames2014.txt", "w") as boys,open("girlnames2014.txt","w") as girls:
    # Note this will not work for name which has speacial charecters like `-,$,etc`
    boy_regex = re.compile(r"^\d\s?([a-zA-z0-9]+)\s[a-zA-z0-9]+$",re.MULTILINE)
    girl_regex = re.compile(r"^\d\s?[a-zA-z0-9]+\s([a-zA-z0-9]+)$",re.MULTILINE)
    boys.write('\n'.join(boy_regex.findall(f1.read())))
    girls.write('\n'.join(girl_regex.findall(f1.read())))

Upvotes: 0

Neeraj
Neeraj

Reputation: 1817

You have specified the same variable name both the output files. outfile.

    infile = open("babynames2014.txt", "r")    
    outfileb = open("boynames2014.txt", "w")
    outfileg = open("girlnames2014.txt", "w")


    line = infile.readline()
    datafield = line.split()
    boyname2014 = datafield[0]
    girlname2014 = datafield[1]
    outfileb.write(boyname2014)
    outfileg.write(girlname2014)


    infile.close()
    outfileb.close()
    outfileg.close()

and you need to loop through the input file in order to get all the names. You can use ''.join([i for i in s if not i.isdigit()]) to remove the number from the names.

    infile = open("babynames2014.txt", "r")    
    outfileb = open("boynames2014.txt", "w")
    outfileg = open("girlnames2014.txt", "w")


    tmp = infile.readline()
    line=''.join([i for i in tmp if not i.isdigit()])
    datafield = line.split()
    boyname2014 = datafield[0]
    girlname2014 = datafield[1]
    outfileb.write(boyname2014)
    outfileg.write(girlname2014)


    infile.close()
    outfileb.close()
    outfileg.close()

Upvotes: 0

MSD
MSD

Reputation: 1407

Here you go,

#! /usr/bin/python
import sys

boy_file = str(sys.argv[1])
girl_file = str(sys.argv[2])

all_records = [line.strip() for line in open('babynames2014', 'r')]

f1 = open(boy_file, "w")
f2 = open(girl_file, "w")

for record in all_records:
        split_record = record.split(' ')
        boy_name = split_record[1]
        girl_name = split_record[2]

        f1.write(boy_name+"\n")
        f2.write(girl_name+"\n")


f1.close()
f2.close()

Upvotes: 0

parsethis
parsethis

Reputation: 8078

readline() only reads a single line (as the name might suggest) so only the first line get read (1 Noah Emma )

To read the all the lines and split them and write them to a file try:

# use two different names for the files
# you had one name `outfile` which was being 
# overwritten so tht why boy file was empty

infile = open("babynames2014.txt", "r") 
boyfile = open("boynames2014.txt", "w") 
girlfile = open("girlnames2014.txt", "w")

with open('babynames2014', 'r') as f:
    for l in f.readlines(): # notice readlines instead of readline
        _, boy, girl = l.split() # assumes the separator is a space
        print(boy, file=boyfile)
        print(girl, file=girlfile)

# don't forget to close your file desciptors
boyfile.close()
girlfile.close()

Upvotes: 0

abhinav
abhinav

Reputation: 1138

I've noticed one thing that is logically not correct i.e., outfile for both boynames2014.txt and girlnames2014.txt

You should've done like this.

infile = open("babynames2014.txt", "r")    
outfile_boys = open("boynames2014.txt", "w")
outfile_girls = open("girlnames2014.txt", "w")

Then, you have to read the infile and split by new line for required data as following.

lines = infile.read().split("\n")

Then iterate over the lines as below and split by space(default).

for line in lines:
    datafield = line.split()
    boyname2014 = datafield[1]
    girlname2014 = datafield[2]
    outfile_boys.write(boyname2014 + '\n')
    outfile_girls.write(girlname2014 + '\n')

I've selected 1 and 2 index for data field because your file contains data like :

1 boy_name girl_name

Splitting by space delivers boy_name to 1st index and girl_name to 2nd index

Then close your files as usual.

infile.close()
outfile_boys.close()
outfile_girls.close()

Hope it helps!

Upvotes: 1

cur4so
cur4so

Reputation: 1820

    outfile1 = open("boynames2014.txt", "w")
    outfile2 = open("girlnames2014.txt", "w")
    with open('babynames2014.txt') as infile:
        for line in infile:
            datafield = line.split()
            boyname2014 = datafield[0]
            girlname2014 = datafield[1]
            outfile1.write(boyname2014)
            outfile2.write(girlname2014)
    outfile1.close()
    outfile2.close()

Upvotes: 0

Akshay Apte
Akshay Apte

Reputation: 1653

You need to have seperate pointers for output files.

`
infile = open("babynames2014.txt", "r")
outfileboy = open("boynames2014.txt", "w")
outfilegirl = open("girlnames2014.txt", "w")
for line in infile.readlines():
    names = line.split(" ")
    outfileboy.write(str(names[1]+"\n")
    outfilegirl.write(str(names[2]+"\n")

outfileboy.close()
outfilegirl.close()

`

Upvotes: 0

Related Questions