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

PythonFileCsvIteratorNext

Python Problem Overview


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

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

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])

Python Solutions


Solution 1 - Python

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

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

Solution 2 - Python

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)

Solution 3 - Python

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

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.

Solution 4 - Python

From the Python documentation:

> 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.

Solution 5 - Python

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

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

Solution 6 - Python

The simple way to get any row in csv file

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

Solution 7 - Python

> 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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandrebrutonView Question on Stackoverflow
Solution 1 - PythonAshwini ChaudharyView Answer on Stackoverflow
Solution 2 - Pythondm03514View Answer on Stackoverflow
Solution 3 - PythonAravind KrishnakumarView Answer on Stackoverflow
Solution 4 - PythonRobert ElwellView Answer on Stackoverflow
Solution 5 - PythonEmerson PetersView Answer on Stackoverflow
Solution 6 - PythonOscar.ChouView Answer on Stackoverflow
Solution 7 - PythonBiplob DasView Answer on Stackoverflow