Question:
how to read in certain objects from a list in python?
Damon
2019-06-28 21:52:10 UTC
I'm trying to read in certain information in python but can't figure out how to loop it or if it's possible the way i'm doing it.


infile = open("C:\\TestData\\TXBabyNames.txt")
lines = infile.readlines()
infile.close()
girls1910=0
##0=st,1=m/f 2= year 3= name, 4 = how many

index=0

for girls in lines:
----elements = lines[index].split(",")
----index +=1
----while elements[2] == "1910" and elements[1]=="F":
--------var1 = elements[4]
--------girls1910+= var1

So what I'm trying to do is grab the index and if the year is 1910 and it's a female which is the first index to keep adding up index 4. I tried multiple ways. and I also get an error for the girls1910 += var1 for adding an int and str. Any help would be appreciated. An explanation would be SOOO helpful.
Three answers:
2019-07-01 02:56:07 UTC
girls1910 = 0

for girls in lines:

.. elements = girls.split(",")

.. if "1910" == elements[2] and "f" == elements[1].lower():

.. .. girls1910 += int(elements[4])
husoski
2019-06-29 08:06:01 UTC
EddieJ already seems to have solved your problem. Using int() [or float() if the data has decimal fractions] is what I'd have suggested, too. Think about awarding best answer based on that...first with the correct fix.



I gave him a thumbs up, and I'd give you a gold star for indenting your Python in the question. Sadly, there's no voting on questions, so what I'll offer instead is a peek into the future, assuming you continue to develop your Python skills.



You have a text file with each line being a "record" and each line has a list of fields separated by commas. That's a common enough format to have a name: "comma-separated value" or "CSV" file. When you see a filetype suffix of ".csv", that's probably a CSV file. Spreadsheet programs (like Excel and Libre Office Calc will export spreadsheets in this format, as will many database programs.



Python has a module you can import to handle this format, and it recognizes a few different styles of quoting fields (needed if a field has a comma included). A csv.reader object acts as a sequence of lists, one for each record, that you can use in a for loop or in list comprehension and generator expressions.



Your program could be simply:



import csv

girls1910 = 0

the_file = open("the_file.txt) )

for line in csv.reader(the_file):

.... if len(line) >= 5 and line[2] == '1910': # notice string comparison

.... .... girls1910 += int(line[4]) # or float(line[4]) if you need fractions

the_file.close()



The "acts as a sequence" part lets you shorten that up quite a bit using the built-in sum() function and a generator expression:



import csv

with open("the_file.txt") as the_file:

.... girls1910 = sum(int(line[4]) for line in csv.reader(the_file) if len(line) > 4 and line[2] == '1910')



The "with" statement ensures that the file gets closed, no matter what, as soon as the indented block is done. I don't expect you to get all of what's in that third line, but that's a sum() call on a generator expression. It takes each list returned by the csv.reader() object, filters out only those that have at least 5 columns and '1910' in the third column, then converts the fifth column to integer and passes just those int values to sum(), and sum() adds all of them up.
EddieJ
2019-06-28 22:15:21 UTC
You don't actually need var1.



You could have

girls1910 += int (elements [4])



The main thing is to convert the characters into an int.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...