Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:使用Excel CSV文件仅读取某些列和行_Python_Excel_File - Fatal编程技术网

Python:使用Excel CSV文件仅读取某些列和行

Python:使用Excel CSV文件仅读取某些列和行,python,excel,file,Python,Excel,File,虽然我可以读取csv文件而不是读取整个文件,但如何仅打印某些行和列 假设这是Excel: A B C D E State |Heart Disease Rate| Stroke Death Rate | HIV Diagnosis Rate |Teen Birth Rate Alabama 235.5 54.5

虽然我可以读取csv文件而不是读取整个文件,但如何仅打印某些行和列

假设这是Excel:

  A              B              C                  D                    E
State  |Heart Disease Rate| Stroke Death Rate | HIV Diagnosis Rate |Teen Birth Rate

Alabama     235.5             54.5                 16.7                 18.01

Alaska      147.9             44.3                  3.2                  N/A    

Arizona     152.5             32.7                 11.9                  N/A    

Arkansas    221.8             57.4                 10.2                  N/A    

California  177.9             42.2                  N/A                  N/A    

Colorado    145.3             39                    8.4                 9.25    
以下是我所拥有的:

import csv

try:
    risk = open('riskfactors.csv', 'r', encoding="windows-1252").read() #find the file

except:
    while risk != "riskfactors.csv":  # if the file cant be found if there is an error
    print("Could not open", risk, "file")
    risk = input("\nPlease try to open file again: ")
else:
    with open("riskfactors.csv") as f:
        reader = csv.reader(f, delimiter=' ', quotechar='|')

        data = []
        for row in reader:# Number of rows including the death rates 
            for col in (2,4): # The columns I want read   B and D
                data.append(row)
                data.append(col)
        for item in data:
            print(item) #print the rows and columns
我只需要阅读包含所有统计数据的B列和D列,就可以这样阅读:

  A              B                D                    
 State  |Heart Disease Rate| HIV Diagnosis Rate |

 Alabama       235.5             16.7                

  Alaska       147.9             3.2                     

  Arizona      152.5             11.9                     

  Arkansas     221.8             10.2                    

 California    177.9             N/A                     

 Colorado      145.3             8.4                
编辑 没有错误

有没有办法解决这个问题?我尝试的一切都不起作用。非常感谢您的任何帮助或建议

试试这个

data = []
for row in reader:# Number of rows including the death rates
    data.append([row[1],row[3]) # The columns I want read  B and D
for item in data
            print(item) #print the rows and columns

我希望你听说过熊猫的资料分析

下面的代码将完成读取列的工作,但是关于读取行,您可能需要进行更多解释

import pandas
io = pandas.read_csv('test.csv',sep=",",usecols=(1,2,4)) # To read 1st,2nd and 4th columns
print io 

如果您仍然被卡住,那么没有理由使用CSV模块读取文件,因为所有CSV文件都是逗号分隔的字符串。所以,对于一些简单的事情,你可以试试这个,它会给你一个形式的元组列表(状态,心脏病发病率,HIV诊断率)


请注意,如果要进行任何类型的数据分析,则必须遍历并忽略标题行。

TypeError:append()只接受一个参数(给定2个),您需要读取和计算错误。不要忽视它们。我意识到了这一点,并做出了改变,但现在它打印了号码和整个列表。你知道,几周前我尝试在不同的代码上导入熊猫,我不断地得到回溯(最近的一次调用):
文件“C:/Users/Thomas/Desktop/Project 1.py”,第33行,在
import pandas`
import恐怖:没有名为“pandas”的模块
您是否在pc中安装了pandas,并使用类似“import pandas”的语句进行了调用您当然是对的。在我经历了地狱般的安装之后,使用
import pandas
时,这让我轻松了!感谢安装和使用导入熊猫后。这运行没有问题。谢谢你的指导。这是非常直截了当的我被这件事耽搁了一天。
output = []

f = open( 'riskfactors.csv', 'rU' ) #open the file in read universal mode
for line in f:
    cells = line.split( "," )
    output.append( ( cells[ 0 ], cells[ 1 ], cells[ 3 ] ) ) #since we want the first, second and third column

f.close()

print output