在python中使字典列表中的值小写

时间:2017-04-15 01:42:51

标签: python-3.x

Picture of file所以,我正在制作这个程序,你拿一个文件并将其分解并取出你需要的特定部分,然后将这些值添加到字典中,然后将该字典附加到一个列表中。执行此操作后,您将搜索您拥有的词典列表,并根据用户提供的信息提取某些值。到目前为止,我已经完成了所有工作,但现在我必须将字典列表中的所有内容都设为小写,但字典列表中包含不同的值类型,我不知道如何去做。

这是我的代码:

#!/usr/bin/env python3
import os.path

def get_file_name(file_name):
    if os.path.exists(file_name):
        return file_name
    else:
        file_name = input('Please enter a file name: ')
        get_file_name(file_name)

def search_counties(new_list):
    c = [line.strip(',') for line in new_list]
    texts = [[word.lower() for word in text.split(',')] for text in c]
    print(c)
    county = input('Search: ')
    county.lower()
    while county not in ['q', 'quit']:
        for row in new_list:
            county_name, county_count, county_percent, county_median = row.split(',')

            if county == county_name:
                pass





    '''
    while that search term is not q or quit enter a loop
        1. iterate over your counties parameter to see if any of the county's names match the search term
            ** You may need to convert your search term to lowercase and compare it to the county name converted to lowercase
        2. if you find a match then call print_county and pass it your county dict
        3. ask the user for another search term (gives the user a chance to search again or q or quit to exit)
    '''

def print_highest_data(counties):
    # This function is complete
    # since we sorted our list of counties in main() all we have to do here is call print_county and pass it the last county dict in our list
    print_county(counties[-1])


def print_lowest_data(counties):
    print_county(counties[0])
    # same as above, except we call print_county and pass it the first county dict in our list


def print_county(county):
    # This function is complete 
    # if you use different keys in your dicts then you'll need to change them below
    print('Name: %s' % county['name'])
    print('Percentage: %.2f%%' % county['percent'])
    print('Children in Poverty: {:,}'.format(county['count']))
    print('Household Income: ${:,}'.format(county['income']))
    print()

def main():

    new_list=[]
    file_name = input('Please enter a file name: ')
    file = get_file_name(file_name)
    file = open(file_name, 'r')
    first_line = True
    for row in file:
        if first_line == True:
            first_line = False
            continue
        dictionary={}
        dictionary['name']= str(row[193:238])
        dictionary['count']= int(row[50:57])
        dictionary['percent']= float(row[76:80])
        dictionary['income']= int(row[134:139])
        new_list.append(dictionary)
    file.close()
    new_list = sorted(new_list, key=lambda x: x['percent'])
    print_highest_data(new_list)
    print_lowest_data(new_list)
    search_counties(new_list)









    # prompt for the filename

    # check to make sure file exists, if it doesn't prompt again

    # create an empty list to contain all of the contains

    ''' 
    iterate over each line in the file (**you may need to skip the first line if it contains a header)
         For each line parse out the children count, percentage of children in poverty, median income, and county name
         **For help parsing, view the census.gov page (http://www.census.gov//did/www/saipe/data/statecounty/data/2014.html)
         Add those four values to a dict (this dict will represent the county)
         append that dict to your list
    '''

    '''
    once you leave your for loop sort the counties by percentage
    **see the lab instructions for guidance on how to sort the dict by a particular value
    '''

    # call print_highest_data and pass it your list of counties as a parameter

    # call print_lowest_data and pass it your list of counties as a parameter

    # call search_counties and pass it your list of counties as a parameter




# if we are running this script then call the main function
if __name__ == '__main__':
    main()

0 个答案:

没有答案