Python AttributeError

时间:2017-06-17 03:23:07

标签: python

# Process the data by running the function we wrote above.
station_data = ['201402_station_data.csv']
trip_in = ['201309_trip_data.csv']
trip_out = '201309_trip_summary.csv'
summarise_data(trip_in, station_data, trip_out)

# Load in the data file and print out the first few rows
sample_data = pd.read_csv(trip_out)
display(sample_data.head())​
# Verify the dataframe by counting data points matching each of the time
features.question_3(sample_data)
AttributeError                            
Traceback (most recent call last)

<ipython-input-15-568a9b76d0da> in <module>()
       3 trip_in = ['201309_trip_data.csv']
       4 trip_out = '201309_trip_summary.csv'
       5 summarise_data(trip_in, station_data, trip_out)
       6 
       7 # Load in the data file and print out the first few rows

       <ipython-input-6-8a5a5140f3e9> in summarise_data(trip_in,     
       station_data, trip_out)
       30### Question 3a: Add a mathematical operation below   ###
       31### to convert durations from seconds to minutes.     ###
       32new_point['duration'] = float(row['Duration']). ________
       33 
       34# reformat datestrings into multiple columns
       enter code here
       AttributeError: 'float' object has no attribute '________'

2 个答案:

答案 0 :(得分:-1)

您的错误在第32行:defaultdict(<class 'dict'>, { 'key1': { 'length_A': '663', 'length_B': '389'}, 'key2': { 'length_A': '865', 'length_B': '553'}})您的代码类型将32new_point['duration'] = float(row['Duration']). ________的值转换为浮点数,然后使用句点(row['duration'])调用属性/或方法。您的代码中没有浮动属性.。正如Klaus D指出的那样,看起来你正在使用模板,并没有像你想象的那样填写值。如果您可以更全面地阅读模板说明或在此处发布更多代码,则可以获得更好的支持。

看起来这就是函数'________'中的全部内容,所以如果你需要更多的帮助,你应该从该函数中编写更多的上下文。

答案 1 :(得分:-1)

def summarise_data(trip_in, station_data, trip_out):
"""
This function takes trip and station information and outputs a new
data file with a condensed summary of major trip information. The
trip_in and station_data arguments will be lists of data files for
the trip and station information, respectively, while trip_out
specifies the location to which the summarized data will be written.
"""
# generate dictionary of station - city mapping
station_map = create_station_mapping(station_data)

with open(trip_out, 'w') as f_out:
    # set up csv writer object        
    out_colnames = ['duration', 'start_date', 'start_year',
                    'start_month', 'start_hour', 'weekday',
                    'start_city', 'end_city', 'subscription_type']        
    trip_writer = csv.DictWriter(f_out, fieldnames = out_colnames)
    trip_writer.writeheader()

    for data_file in trip_in:
        with open(data_file, 'r') as f_in:
            # set up csv reader object
            trip_reader = csv.DictReader(f_in)

            # collect data from and process each row
            for row in trip_reader:
                new_point = {}

                # convert duration units from seconds to minutes
                ### Question 3a: Add a mathematical operation below   ###
                ### to convert durations from seconds to minutes.     ###
                new_point['duration'] = float(row['Duration'])/60
                # reformat datestrings into multiple columns
                ### Question 3b: Fill in the blanks below to generate ###
                ### the expected time values.                         ###
                trip_date = datetime.strptime(row['Start Date'], '%m/%d/%Y %H:%M')
                new_point['start_year']  = trip_date.strftime('%Y')
                new_point['start_month'] = trip_date.strftime('%m')
                new_point['start_hour']  = trip_date.strftime('%H')
                new_point['weekday']     = trip_date.strftime('%A')

                # remap start and end terminal with start and end city
                new_point['start_city'] = station_map[row['Start Terminal']]
                new_point['end_city'] = station_map[row['End Terminal']]
                # two different column names for subscribers depending on file
                if 'Subscription Type' in row:
                    new_point['subscription_type'] = row['Subscription Type']
                else:
                    new_point['subscription_type'] = row['Subscriber Type']

                # write the processed information to the output file.
                trip_writer.writerow(new_point)


    # Process the data by running the function we wrote above.
    station_data = ['201402_station_data.csv']
    trip_in = ['201309_trip_data.csv']
    trip_out = '201309_trip_summary.csv' 
    summarise_data(trip_in, station_data, trip_out)

    # Load in the data file and print out the first few rows
    sample_data = pd.read_csv(trip_out)
    display(sample_data.head())

    # Verify the dataframe by counting data points matching each of the      
    time features.

enter image description here