Reputation: 129
I have a text file named headerValue.txt which contains the following data:-
Name Age
sam 22
Bob 21
I am trying to write a python script that would add a new header called 'Eligibility'. It would read the lines from the text file and check if the age is more than 18, then it will print 'True' underneath 'Eligibility' header and also add one more header named 'Adult' and print a 'yes' underneath and 'no' if the age is less than 18.
Since in the text file the age of both is more than 18. The output will something be like:-
Name Age Eligibility Adult
sam 22 True yes
Bob 21 True yes
code snippet:-
fo =open("headerValue.txt", "r")
data = fo.readlines()
for line in data:
if "Age" in line:
if int(Age) > 18:
Eligibility = "True"
Adult = "yes"
I am kinda stuck here since I am new to python. Any help how to update the text file and add headers with its values?Thanks
Upvotes: 0
Views: 4073
Reputation: 12077
You're trying to check if the string "Age"
exists in a line. It only exists on the first line. Then you try to convert a variable Age
to an integer and see if it's larger than 18. I can't see that you've defined Age
anywhere so I can't tell if this code runs.
It seems like you have a tab-delimited or a space-delimited file. It looks like a CSV-file so you might as well treat it as such. It's easier, because python provides a builtin csv
module. In that module there's the csv.DictReader
and csv.DictWriter
objects which allow you to modify each line in the csv-file as a dictionary. In the below file, the csv.DictWriter
gets default header names and the restval
-parameter defines the default parameter for the fields. This simplifies the code as you can see below:
>>> with open("test.csv", "r") as inf, open("out.csv", "w") as outf:
... in_reader = csv.DictReader(inf, delimiter=" ")
... out_writer = csv.DictWriter(outf, delimiter=" ", fieldnames=['Name', 'Age', 'Eligibility', 'Adult'], restval=False)
... out_writer.writeheader()
... for line in in_reader:
... if int(line['Age']) > 18:
... line['Eligibility'] = True
... line['Adult'] = True
... out_writer.writerow(line)
Input file is test.csv
:
msvalkon@lunkwill~$: cat test.csv
Name Age
sam 22
Bob 21
foo 5
Output in out.csv
:
msvalkon@lunkwill~$: cat out.csv
Name Age Eligibility Adult
sam 22 True True
Bob 21 True True
foo 5 False False
Upvotes: 1
Reputation: 14955
This module is suited for you : https://docs.python.org/2/library/fileinput.html?highlight=fileinput#fileinput
For a infile edition you can use:
#!/usr/bin/python
import fileinput
cont = 0
for line in fileinput.input("./path/to/file", inplace=True):
if not cont:
name = "Name"
age = "Age"
Eligibility= "Eligibility"
Adult = "Adult"
else:
name ,age = line.split()
if int(age) > 18:
Eligibility = "True"
Adult = "yes"
print "{0}\t{1}\t{2}\t{3}".format(name, age, Eligibility, Adult)
cont += 1
This , will update your original file.
Upvotes: 0
Reputation: 11734
fo =open("headerValue.txt", "r")
data = [l.split() for l in fo.readlines()]
headers = data[0]
headers.extend(('Eligibility', 'Adult'))
ageind = headers.index('Age')
for line in data[1:]:
if int(line[ageind]) > 18:
line.extend(('True', 'yes'))
else:
line.extend(('False', 'no'))
The if
in your code would only be true for the first line (not the one you're interested in). I replaced it with checking for the index of age. Also, usually variable names are not capitalized.
Also, regarding data = [l.split() for l in fo.readlines()]
- it looks like your file is space separated. If it's tab/comma separated you can split accordingly, but I suggest using csv.
Upvotes: 1