Python Forum
Need help improving function that reads file into list of tuples
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help improving function that reads file into list of tuples
#1
Hi everyone,

I'm having some slight difficulty getting this function to do exactly what I need it to do.

Essentially it reads in a file.csv, and needs to store various columns into a "master_list" of tuples.

My function works, but I'm sure could be improved. It does not return exactly the output I need.

These are the columns of interest:
Quote:year = int(line[2])
month = int(line[3])
magnitude = float(line[9])
location = line[19]
latitude = float(line[20])
longitude = float(line[21])
deaths = int(line[23])
missing = int(line[25])
injuries = int(line[27])
damages = float(line[29])

and the requirement:
Quote:If the number of deaths, missing, injured, and damages columns are empty, replace
it with a zero. If any other numerical data cannot be made into an int or float,
skip that entire line of data. Create a tuple with items in this order:
tup = (year,month,magnitude,location,latitude,longitude,\
deaths,missing,injuries,damages)

Here's my code:

def read_file(fp):
    next(fp, None)
    masterList = []
    tup = ()

    for col in csv.reader(fp, delimiter=',', skipinitialspace=True):
        year = col[2]
        month = col[3]
        magnitude = col[9]
        location = col[19]
        latitude = col[20]
        longitude = col[21]
        deaths = col[23]
        missing = col[25]
        injured = col[27]
        damages = col[29]

        try:
            year = int(year)
            month = int(month)
            magnitude = float(magnitude)
            latitude = float(latitude)
            longitude = float(longitude)
        except:
            continue
            
                    
        if deaths.isdigit() == True:
            if int(deaths) > 0:
                deaths = int(deaths)
        elif deaths == '':
            deaths = int('0')
        else:
            deaths = int('0')
            
        if missing.isdigit() == True:
            if int(missing) > 0:
                missing = int(missing)
        elif missing == '':
            missing = int('0')
        else:
            missing = int('0')
            
            
        if injured.isdigit() == True:
            if int(injured) > 0:
                injured = int(injured)
        elif injured == '':
            injured = int('0')
        else:
            injured = int('0')
            
        if isinstance(damages, float) == True:
            try:
                damages = float(damages)
                if damages:
                    damages = int('0')
            except:
                damages = int('0')
I think the most major problem with my function right now is that the last column (damages) does not return the correct value all of the time. If the cell in the csv is blank, it needs to be a 0. If not, it needs to read the float. I can't seem to get this right, can anybody offer some suggestions?


Expected output:
[(2020, 1, 6.0, 'CHINA:  XINJIANG PROVINCE', 39.831, 77.106, 1, 0, 2, 0), (2020, 1, 6.7, 'TURKEY:  ELAZIG AND MALATYA PROVINCES', 38.39, 39.081, 41, 0, 1600, 0), (2020, 1, 7.7, 'CUBA: GRANMA;  CAYMAN IS;  JAMAICA', 19.44, -78.755, 0, 0, 0, 0), (2020, 2, 6.0, 'TURKEY: VAN;  IRAN', 38.482, 44.367, 10, 0, 60, 0), (2020, 3, 5.4, 'BALKANS NW:  CROATIA:  ZAGREB', 45.897, 15.966, 1, 0, 27, 6000.0), (2020, 3, 5.7, 'USA: UTAH', 40.751, -112.078, 0, 0, 0, 48.5)]
My output (notice the last float in the tuple is wrong):

[(2020, 1, 6.0, 'CHINA:  XINJIANG PROVINCE', 39.831, 77.106, 1, 0, 2, 0), (2020, 1, 6.7, 'TURKEY:  ELAZIG AND MALATYA PROVINCES', 38.39, 39.081, 41, 0, 1600, 0), (2020, 1, 7.7, 'CUBA: GRANMA;  CAYMAN IS;  JAMAICA', 19.44, -78.755, 0, 0, 0, 0), (2020, 2, 6.0, 'TURKEY: VAN;  IRAN', 38.482, 44.367, 10, 0, 60, 0), (2020, 3, 5.4, 'BALKANS NW:  CROATIA:  ZAGREB', 45.897, 15.966, 1, 0, 27, 6000.0), (2020, 3, 5.7, 'USA: UTAH', 40.751, -112.078, 0, 0, 0, 0)]
Reply


Messages In This Thread
Need help improving function that reads file into list of tuples - by t4keheart - Nov-02-2020, 10:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding values with reduce() function from the list of tuples kinimod 10 2,814 Jan-24-2023, 08:22 AM
Last Post: perfringo
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,565 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Count number of occurrences of list items in list of tuples t4keheart 1 2,422 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  Improving my understanding of functions and methods menator01 2 2,203 Apr-24-2020, 06:26 AM
Last Post: menator01
  Dictionary and tuples list comprehensions help paul41 2 2,445 Nov-29-2019, 06:59 PM
Last Post: perfringo
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 2,675 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 2,365 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Converting List into list of tuples ARV 4 4,851 Sep-28-2019, 04:58 AM
Last Post: perfringo
  Improving code to autorename if filename exists Den0st 5 3,043 Sep-23-2019, 07:53 AM
Last Post: wavic
  reading txt file putting in list function Expel 7 3,872 Jul-17-2019, 03:18 PM
Last Post: Expel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020