Python将字符串转换为int [重复]
Posted
技术标签:
【中文标题】Python将字符串转换为int [重复]【英文标题】:Python Converting str to int [duplicate] 【发布时间】:2021-12-25 15:28:48 【问题描述】:我的程序需要一个从 csv 文件(“all.csv”)读取数据并提取与特定日期的状态相关的所有数据的函数(提取具有“状态名称”和“日期”的每一行它),然后将提取的数据写入另一个名为:state +“.csv”的csv文件
在写入数据时,会计算并汇总每个州在该特定日期的病例数和死亡数。然后该函数将总病例数和死亡数作为元组返回 (cases,deaths)
例如。状态 = '加利福尼亚' 日期 = '2020-03-09'
我得到的错误是 '0.0' 和 'deaths' 不能转换为 int。第一行是标题,我收到“死亡无法转换为 int”的错误。所以我有两个问题:
-
如何跳过标题“死亡”(最后一列)并转到其余数据?
如何将其余数据(十进制格式的字符串)转换为 int?
注意:当我将链接数据保存到“all.csv”时,死亡列转换为十进制格式 (0.0)。
这里是“all.csv”的内容:https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv
这是 'all.csv' 的 sn-p: **请注意,“all.csv”中有 7 列,而不是 csv 文件超链接中的 6 列
这是我写的程序:
import csv
input_file = 'all.csv'
state = input()
date = input() # date format m/d/yyyy
output_file = state + '.csv'
def number_of_cases_deaths_by_date(input_file, output_file, state, date):
with open(input_file, 'r') as infile: #open both files
contents = infile.readlines()
with open(output_file, 'w') as outfile:
writer = csv.writer(outfile)
for row in range(len(contents)): # save data in list
contents[row] = contents[row].split(',') #split elements
contents[row][6] = contents[row][6].strip('\n') #strip \n from last column
print(contents[3:5])
cases = 0
deaths = 0
for row in range(len(contents)):
if contents[row][3] == state and contents[row][1] == date: # if row has desired state, write it to new file
writer.writerow((contents[row]))
int_cases = int(contents[row][5])
cases = cases + int_cases
int_deaths = int(contents[row][6])
deaths += deaths + int_deaths
return (cases, deaths)
data = number_of_cases_deaths_by_date(input_file, output_file, state, date)
print(data)
【问题讨论】:
Please do not post text as images。将文本复制并粘贴到您的问题中,并使用代码格式化工具正确格式化。图像不可搜索,也无法通过屏幕阅读器为有视觉障碍的人解读。使用edit 链接修改您的问题。 我认为你可以使用 pandas、numpy 库来玩 CSV 数据 "如何将其余数据(十进制格式的字符串)转换为 int?"用您自己的话来说,您认为int
是什么?用您自己的话来说,为什么您认为应该可以将“十进制格式的字符串”转换为 1,您认为应该是什么结果?
“我怎样才能跳过标题“死亡”(最后一列)并转到其余数据?”您是否尝试阅读 csv 模块的文档?把python csv skip header
into a search engine放上去怎么样?
【参考方案1】:
没有说明为什么all.csv
数据与您的链接不同,但清理您的代码并使用原始下载数据很简单:
import csv
def number_of_cases_deaths_by_date(input_file, output_file, desired_state, desired_date):
total_cases = 0
total_deaths = 0
with open(input_file, encoding='utf8', newline='') as infile, \
open(output_file, 'w', encoding='utf8', newline='') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
header = next(reader)
for date,county,state,fips,cases,deaths in reader:
if state == desired_state and date == desired_date:
writer.writerow([date,county,state,fips,cases,deaths])
total_cases += int(cases)
total_deaths += int(deaths)
return total_cases, total_deaths
input_file = 'us-counties.csv'
state = 'Oregon'
date = '2021-11-05'
output_file = f'state.csv'
data = number_of_cases_deaths_by_date(input_file, output_file, state, date)
print(data)
输出:
(372137, 4562)
俄勒冈州.csv:
2021-11-05,Baker,Oregon,41001,2079,30
2021-11-05,Benton,Oregon,41003,5779,31
2021-11-05,Clackamas,Oregon,41005,31022,328
2021-11-05,Clatsop,Oregon,41007,2491,29
2021-11-05,Columbia,Oregon,41009,4024,47
2021-11-05,Coos,Oregon,41011,5294,98
2021-11-05,Crook,Oregon,41013,3114,53
2021-11-05,Curry,Oregon,41015,1846,27
2021-11-05,Deschutes,Oregon,41017,21677,138
2021-11-05,Douglas,Oregon,41019,12622,263
2021-11-05,Gilliam,Oregon,41021,168,4
2021-11-05,Grant,Oregon,41023,1039,14
2021-11-05,Harney,Oregon,41025,1172,30
2021-11-05,Hood River,Oregon,41027,2007,37
2021-11-05,Jackson,Oregon,41029,23807,330
2021-11-05,Jefferson,Oregon,41031,3972,60
2021-11-05,Josephine,Oregon,41033,9712,206
2021-11-05,Klamath,Oregon,41035,8552,127
2021-11-05,Lake,Oregon,41037,987,15
2021-11-05,Lane,Oregon,41039,28799,323
2021-11-05,Lincoln,Oregon,41041,3352,45
2021-11-05,Linn,Oregon,41043,13711,141
2021-11-05,Malheur,Oregon,41045,5814,82
2021-11-05,Marion,Oregon,41047,38270,472
2021-11-05,Morrow,Oregon,41049,1897,24
2021-11-05,Multnomah,Oregon,41051,57661,746
2021-11-05,Polk,Oregon,41053,7737,87
2021-11-05,Sherman,Oregon,41055,175,3
2021-11-05,Tillamook,Oregon,41057,2045,38
2021-11-05,Umatilla,Oregon,41059,14820,164
2021-11-05,Union,Oregon,41061,3294,50
2021-11-05,Wallowa,Oregon,41063,729,12
2021-11-05,Wasco,Oregon,41065,3033,41
2021-11-05,Washington,Oregon,41067,40121,342
2021-11-05,Wheeler,Oregon,41069,108,1
2021-11-05,Yamhill,Oregon,41071,9207,124
【讨论】:
OP 提出了两个问题,这两个问题都是现成的,也很容易研究。这个问题唯一的好处是它提供了足够的信息来回答。请不要尝试这样的答案,尤其是没有任何解释的情况下。 Stack Overflow 不是代码编写服务。以上是关于Python将字符串转换为int [重复]的主要内容,如果未能解决你的问题,请参考以下文章