从csv文件中读取第二列以列出 [英] Reading the second column from csv file to list

查看:113
本文介绍了从csv文件中读取第二列以列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两列的csv文件.我试图将第二个列从该csv文件读取到python中的列表.我提到了stackoverflow中的一些方法.我尝试过,但出现错误.

I have a csv file with two columns. I am trying to read that second column from that csv file to a list in python . I referred to some of the ways in stackoverflow. I tried them, but I am getting an error.

noise_amp=[]         #an empty list to store the second column
with open('null_ch1_waveform_10mV.csv', 'rb') as rf:
    reader = csv.reader(rf, delimiter=';')
    for row in reader:
       noise_amp.extend([row[1]])

我收到此错误:

Traceback (most recent call last):
  File "J:/Ramu_Scripts/noise_script/source_code.py", line 58, in <module>
    noise_amp.extend([row[1]])
IndexError: list index out of range

我的csv文件就像

 1,2
 2,3
 3,4
 4,5 

推荐答案

尝试一下:

import csv
noise_amp=[]         #an empty list to store the second column
with open('demo.csv', 'r') as rf:
    reader = csv.reader(rf, delimiter=',')
    for row in reader:
      noise_amp.append(row[1])

使用 r rb 作为打开模式,定界符为

Use r or rb as open mode and delimiter be ,

这篇关于从csv文件中读取第二列以列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆