ValueError: invalid literal for int() with base 10: Height (mm)

2024/11/16 18:05:49
import csv
from decimal import *def mean(data_set):return Decimal(sum(data_set)) / len(data_set)def variance(data_set):mean_res = mean(data_set)differences = []squared_res = []for elem in data_set:differences.append(elem - mean_res)for elem in differences:squared_res.append(elem ** 2)return mean(squared_res)def standard_deviation(data_set):variance_res = variance(data_set)return variance_res ** Decimal('0.5')if __name__ == "__main__":with open("dog_data.csv", "r") as csv_file:csv_reader = csv.reader(csv_file)height_data = []for row in csv_reader:height_data.append(int(row[1]))print "Mean: {}".format(mean(height_data))print "Variance: {}".format(variance(height_data))print "Standard Deviation:{}".format(standard_deviation(height_data))

Here I'm getting an ValueError: invalid literal for int() with base 10: 'Height (mm)'.. What does this mean? How do i tackle this error ?

Answer

I think your problem is that you read the header (first line) which contain words, and you try to parse it as int.

Try to add

next(csv_reader, None)

Before your loop to skip first row.

As to "How do I tackle that error", for next time simply use print:

print row[1]

Right before the line that produced the error. See what you actually try to convert to int..

https://en.xdnf.cn/q/119501.html

Related Q&A

Having trouble in getting page source with beautifulsoup

I am trying to get the HTML source of a web page using beautifulsoup. import bs4 as bs import requests import urllib.request sourceUrl=https://www.pakwheels.com/forums/t/planing-a-trip-from-karachi-to-…

Cannot convert from pandas._libs.tslibs.timestamps.Timestamp to datetime.datetime

Im trying to convert from pandas._libs.tslibs.timestamps.Timestamp to datetime.datetime but the change is not saved: type(df_unix.loc[45,LastLoginDate])OUTPUT: pandas._libs.tslibs.timestamps.Timestampt…

string index out of range Python, Django

im using Django to develop a web application. When i try and run it on my web form i am receiving string index out of range error. However, when i hardcode a dictionary in to a python test file it work…

PyQt UI event control segregation

I am a beginner in python but my OOPS concept from Java and Android are strong enough to motivate me in making some tool in python. I am using PyQt for developing the application. In my application th…

How to re-run process Linux after crash? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about a specific programming problem, a software algorithm, or s…

Number of pairs

I am trying to write a code that takes m. a, a list of integers n. b, an integer and returns the number of pairs (m,n) with m,n in a such that |m-n|<=b. So far, Ive got this def nearest_pairs(a, b):…

xml.etree.ElementTree.ParseError: not well-formed

I have the following code:from xml.etree import ElementTreefile_path = some_file_pathdocument = ElementTree.parse(file_path, ElementTree.XMLParser(encoding=utf-8))If my XML looks like the following it …

Convert nested XML content into CSV using xml tree in python

Im very new to python and please treat me as same. When i tried to convert the XML content into List of Dictionaries Im getting output but not as expected and tried a lot playing around.XML Content<…

How to decode binary file with for index, line in enumerate(file)?

I am opening up an extremely large binary file I am opening in Python 3.5 in file1.py:with open(pathname, rb) as file:for i, line in enumerate(file):# parsing hereHowever, I naturally get an error beca…

how to install pyshpgeocode from git [duplicate]

This question already has answers here:The unauthenticated git protocol on port 9418 is no longer supported(10 answers)Closed 2 years ago.I would like to install the following from Git https://github.c…