Csv Read Specific Row

CSV read specific row

You could use a list comprehension to filter the file like so:

with open('file.csv') as fd:
reader=csv.reader(fd)
interestingrows=[row for idx, row in enumerate(reader) if idx in (28,62)]
# now interestingrows contains the 28th and the 62th row after the header

How to read a specific line of data from a csv file?

A filter should do the trick, something like

import csv
with open('details.csv', 'rt') as f:
reader = csv.reader(f)
selected_details = input("Enter student ID for details:\n")
results = filter(lambda x: selected_details in x, reader)
for line in results:
print(line)
Explanation:

Filter take an itterable (here reader) and will apply for each element of the itterable the lambda you give him and will return a new list "filtered". If the lambda return True the element will be returned in the filtered list.

A lambda is basically a dwarf-function (wiser, nerdier people will correct me on this over simplification but you get the idea) that return the one and only line you gave him.

So my lambda just does the operation "selected_details in x" that will return True if selected_details is in x and else False, you get the idea.

How to read specific rows/columns of a .CSV file and storing them as a numpy matrix?

This should give you an Idea about your problem solving.

import pandas as pd
import numpy as np

data = pd.read_csv("/Users/DHarun/Desktop/STD_MASTER/F_Bildverarbeitung/aim2/iaai/stack/xyz.csv", sep="\s")

del data['DATE']

np.array(data.values)

Output:

array([[1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 2100000,
74],
[1050000, 1050010, 1050000, 1050001, 1050000, 1000000, 1648,
5],
[1030200, 1060000, 1030200, 1044474, 1042265, 1050001, 28469,
108],
[1040001, 1049999, 1040001, 1042303, 1045001, 1044474, 6518,
10],
[1049800, 1050000, 1048600, 1048787, 1050000, 1042303, 277,
11],
[1059973, 1059974, 1052000, 1053807, 1055000, 1048787, 916,
17],
[1050000, 1054498, 1043009, 1048173, 1043009, 1053807, 2098,
29],
[1045678, 1049989, 1040002, 1049961, 1049979, 1048173, 28098,
14],
[1050001, 1053000, 1046700, 1049473, 1046700, 1049961, 5498,
33]])

reading CSV file specific column and row with Python

What you get in csvitem is a list of lists, where the first index is a row number, and the second index is a column number

You are not grabbing a column in your example. You are printing nth element of each row.
To have column as an indexable object you need either to append to a list instead of prinitng in your example (that will only give you that one column as a list) or you can transpose the entire contents of the file, which is more interesting and which is shown below

transposed_csv = list(zip(*csvitem))
# now you have a list of columns, while each column is a tuple of strings
print(transposed_csv[2])

Reading from specific row from a csv file and extract info by removing delimiter

using System;
using System.IO;
using System.Linq;

namespace CsvReader
{
public class CsvRecord
{
public string Col1 { get; set; }
public string Col2 { get; set; }
public string Col3 { get; set; }
};

class Program
{
static void Main(string[] args)
{
var csvLines = File.ReadAllLines("test.csv");

// Define how many lines you want to skip in Scip method parameter (8 - start with line 9)
var csvRecords = csvLines.Skip(8)
.Select(x => {
var items = x.Split(';');

var lastDotIndex = items[0].LastIndexOf('.');
return new CsvRecord
{
Col1 = lastDotIndex != -1 ? items[0].Remove(lastDotIndex) : items[0],
Col2 = items[1],
Col3 = items[2],
};
})
.ToList();

// Just to output result
csvRecords.ForEach(x => Console.WriteLine($"{x.Col1} - {x.Col2} - {x.Col3}"));
}
}
}

Add some checks to avoid exceptions in the part of code where new CsvRecord is created.

Or if you need only first column, then:

using System;
using System.IO;
using System.Linq;

var csvLines = File.ReadAllLines("test.csv");

// Define how many lines you want to skip in Scip method parameter (8 - start with line 9)
var csvRecords = csvLines.Skip(8)
.Select(x => {
var items = x.Split(';');

var lastDotIndex = items[0].LastIndexOf('.');

return lastDotIndex != -1 ? items[0].Remove(lastDotIndex) : items[0];
})
.ToList();

// Just to output result
csvRecords.ForEach(x => Console.WriteLine(x));


Related Topics



Leave a reply



Submit