python - How can i remove all extra characters from list of strings to convert to ints -
hi i'm pretty new programming , python, , first post, apologize poor form.
i scraping website's download counts , receiving following error when attempting convert list of string numbers integers sum. valueerror: invalid literal int() base 10: '1,015'
i have tried .replace() not seem doing anything.
and tried build if statement take commas out of string contains them: does python have string contains substring method?
here's code:
downloadcount = pagehtml.xpath('//li[@class="download"]/text()') downloadcount_clean = [] download in downloadcount: downloadcount_clean.append(str.strip(download)) item in downloadcount_clean: if "," in item: item.replace(",", "") print(downloadcount_clean) downloadcount_clean = map(int, downloadcount_clean) total = sum(downloadcount_clean)
strings not mutable in python. when call item.replace(",", "")
, method returns want, not stored anywhere (thus not in item
).
edit :
i suggest :
for in range(len(downloadcount_clean)): if "," in downloadcount_clean[i]: downloadcount_clean[i] = downloadcount_clean[i].replace(",", "")
second edit :
for bit more simplicity and/or elegance :
for index,value in enumerate(downloadcount_clean): downloadcount_clean[index] = int(value.replace(",", ""))
Comments
Post a Comment