大家好,我正在使用 Python 2.7.3,我正在尝试构建我的输出列表,其中数据在每个类别下打印。
我的数据如下:
('State', 'Heart Disease Death Rate (2007)', 'Motor Vehicle Death Rate (2009)', 'Teen Birth Rate (2009)', 'Adult Smoking (2010)', 'Adult Obesity (2010)')
('Alabama', '235.5', '18.01', '50.7', '21.90%', '33.00%')
('Alaska', '147.9', '9.16', '44.5', '20.40%', '25.20%')
('Arizona', '152.5', '12.24', '50.6', '13.50%', '24.70%')
('Arkansas', '221.8', '20.25', '59.3', '22.90%', '30.90%')
('California', '177.9', '8.34', '36.6', '12.10%', '24.70%')
我需要将列表读取为:
'State', 'Heart Disease', Vehicle Death Rate', 'Teen Birth Rate', 'Adult Smoking , 'Adult Obesity
Alabama' '235.5', 18.01', '50.7', '21.90%', 33.00%'
Alaska, '147.9', '9.16', '44.5', '20.40%', 25.20%
Arizona', '152.5', '12.24', ' 50.6', '13.50%', '24.70%'
Arkansas', '221.8', '20.25', '59.3', '22.90%', '30.90%'
继承人我到目前为止:
import csv
import pandas
import string, re
output = []
file1 = []
risk = open('riskfactors.csv', 'r').read() #find the file
reader = csv.reader(risk, delimiter='\t', quotechar='|')
try:
with open('riskfactors.csv', 'rU') as file1:
for line in file1:
cells = line.split(",")
output.append((cells[0], cells[1], cells[5], cells[7], cells[11], cells[13]))
for elem in output:
print(elem)
没有错误
我需要 2.7.3 因为它是我使用的唯一方法import pandas
。
我应该怎么做才能正确打印?
我觉得我很接近,我错过了什么?任何建议将不胜感激。