如何在 Python 中读取一行 csv 数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17262256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:54:39  来源:igfitidea点击:

How to read one single line of csv data in Python?

pythonfilecsviteratornext

提问by andrebruton

There is a lot of examples of reading csv data using python, like this one:

有很多使用python读取csv数据的例子,比如这个:

import csv
with open('some.csv', newline='') as f:
  reader = csv.reader(f)
  for row in reader:
    print(row)

I only want to read one line of data and enter it into various variables. How do I do that? I've looked everywhere for a working example.

我只想读取一行数据并将其输入到各种变量中。我怎么做?我到处寻找一个有效的例子。

My code only retrieves the value for i, and none of the other values

我的代码只检索 i 的值,而不检索其他任何值

reader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in reader:
  i = int(row[0])
  a1 = int(row[1])
  b1 = int(row[2])
  c1 = int(row[2])
  x1 = int(row[2])
  y1 = int(row[2])
  z1 = int(row[2])

回答by dm03514

you could get just the first row like:

你可以得到第一行,如:

with open('some.csv', newline='') as f:
  csv_reader = csv.reader(f)
  csv_headings = next(csv_reader)
  first_line = next(csv_reader)

回答by Ashwini Chaudhary

To read only the first row of the csv file use next()on the reader object.

要仅读取 csv 文件的第一行,请next()在 reader 对象上使用。

with open('some.csv', newline='') as f:
  reader = csv.reader(f)
  row1 = next(reader)  # gets the first line
  # now do something here 
  # if first row is the header, then you can do one more next() to get the next row:
  # row2 = next(f)

or :

或者 :

with open('some.csv', newline='') as f:
  reader = csv.reader(f)
  for row in reader:
    # do something here with `row`
    break

回答by Robert Elwell

From the Python documentation:

Python 文档

And while the module doesn't directly support parsing strings, it can easily be done:

虽然该模块不直接支持解析字符串,但可以轻松完成:

import csv
for row in csv.reader(['one,two,three']):
    print row

Just drop your string data into a singleton list.

只需将您的字符串数据放入单例列表中即可。

回答by Aravind Krishnakumar

You can use Pandas library to read the first few lines from the huge dataset.

您可以使用 Pandas 库从庞大的数据集中读取前几行。

import pandas as pd

data = pd.read_csv("names.csv", nrows=1)

You can mention the number of lines to be read in the nrows parameter.

您可以在 nrows 参数中提及要读取的行数。

回答by Oscar.Chou

The simple way to get any row in csv file

在csv文件中获取任何行的简单方法

import csv
csvfile = open('some.csv','rb')
csvFileArray = []
for row in csv.reader(csvfile, delimiter = '.'):
    csvFileArray.append(row)
print(csvFileArray[0])

回答by Emerson Peters

Just for reference, a forloop can be used after getting the first row to get the rest of the file:

仅供参考,for在获取第一行后可以使用循环来获取文件的其余部分:

with open('file.csv', newline='') as f:
    reader = csv.reader(f)
    row1 = next(reader)  # gets the first line
    for row in reader:
        print(row)       # prints rows 2 and onward

回答by Biplob Das

To print a range of line, in this case from line 4 to 7

import csv

with open('california_housing_test.csv') as csv_file:
    data = csv.reader(csv_file)
    for row in list(data)[4:7]:
        print(row)

要打印一系列行,在本例中为第 4 行到第 7 行

import csv

with open('california_housing_test.csv') as csv_file:
    data = csv.reader(csv_file)
    for row in list(data)[4:7]:
        print(row)