Check if CSV header exists

python csv check if header exists
powershell check if column exists in csv
python csv write header once
python check if csv has header
powershell import-csv | get header names
python csv header
check if csv file has header
pandas check if header exists

I have class Importer for my Rails application in which I use method to import CSV file.

def import
  CSV.foreach(file.path, headers: true, encoding: "iso-8859-1") do |row|
    mail = row["email"]||row["Email"]||row["e-mail"]||row["E-mail"]||row["mail"]||row["Mail"]
  end
end

I set variable mail to perform actions inside a loop, I try to protect it from different names of mail column, but I have no clue how should break a loop and keep a code DRY in case when there is CSV without column with any of defined headers.

EDIT:

def import
  header = nil
  headers = CSV.open(file.path, encoding: "iso-8859-1") { |csv| csv.first }
  headers.each { |e| header = e if e.downcase.gsub('-','')=~/^(|e)mail$/ }
  if header != nil
    CSV.foreach(file.path, headers: true, encoding: "iso-8859-1") do |row|
      mail = row[header]
    end
  end
end

Solution to the problem

This should get you started. You'll need to change the regexp to match all of your cases.

def import
  CSV.foreach(file.path, headers: true, encoding: "iso-8859-1") do |row|
    if row.headers.none?{|e| e =~ /email/i}
      raise "freak out"
    end
  end
end

I would also consider setting a variable has_email_headers that you can check since you don't want to have to scan every row's header since they are all the same.

How to check if a CSV has a header using Python?, Python has a built in CSV module that could help. E.g. import csv with open(' example.csv', 'rb') as csvfile: sniffer = csv.Sniffer() has_header� Checking for CSV Column Headers in C#. Importing CSV files into a .NET object isn’t too tricky and for this I usually use CsvHelper. However, sometimes I need to import a CSV file and only extract a couple of columns from it and these columns aren’t always guaranteed to exist. In order to do this I use the following code. string filePath = "C:/Temp/test.csv" // Check if file exists. if (File.Exists (filePath)) { // Read column headers from file CsvReader csv = new CsvReader (File

According to the CSV documentation of Ruby 2.5.0 you can also use the return_headers:true to check for the header_row? later in a loop. Here's an example:

data = CSV.read("your.csv", headers: true, return_headers: true)
(0..(data.length-1)).each do |row|
   if  data[row].header_row? then
      p "yes header!"
   end
end

Checking for CSV Column Headers in C# – bitScry, string filePath = "C:/Temp/test.csv" // Check if file exists. if (File.Exists(filePath)) { // Read column headers from file CsvReader csv = new� This script assumes you have a CSV file with the header (first line) with the word ‘users’. Here’s an example CSV file: myusers.csv. Below is the PowerShell script I wrote. I’ve also written about ‘If’ and ‘Else’ before, so read that if you want some clarification. The user list I have is based on User Principal Name (UPN

Once could also try the header_converters: [:downcase, :symbol] option, to just have to check fewer values (i.e., case insensitive), such as [:email, :mail]:

CSV.foreach(file.path, headers: true, header_converters: [:downcase, :symbol], encoding: "iso-8859-1") do |row|
  puts 'You are missing the "email" header!' unless [:email, :mail].all? { |header| row.headers.include? header }
  # refine/refactor as necessary...
  # do rest of function...
end

Documentation on :header_converters.

CSV file header row.. : learnpython, All my data is fine when I open the file in excel but I don't know how to add a header row with python.. I can't seem to find any tutorials on this … According to the CSV documentation of Ruby 2.5.0 you can also use the return_headers:true to check for the header_row? later in a loop. Here's an example: data = CSV.read("your.csv", headers: true, return_headers: true) (0..(data.length-1)).each do |row| if data[row].header_row? then p "yes header!" end end

14.1. csv — CSV File Reading and Writing — Python 3.4.10 , The lack of a well-defined standard means that subtle differences often exist in the data The csv module's reader and writer objects read and write sequences. and return True if the first row appears to be a series of column headers. of the Dialect class having a set of specific methods and a single validate() method. What Niten is suggesting, and I'll second, is that Powershell might not be the best tool for doing this 'validation'. You are going to have to read each line from the CSV file, and then perform custom checks on it, based on your own needs, like not allowing certain characters, checking length, if each element is supplied, etc.

Python: Read a CSV file line by line with or without header , How to check if a file or directory or link exists in Python ? How to create multi line string objects in python ? How to write data in a CSV file in C++� VBA to check if the these column header are there or not in the csv file I am looking for a VBA code to run from access to find if the following columns headers are available in the C:\MachineList.csv or not and also to be in the same order :

chkcsv -- Check the format and content of a CSV file, chkcsv.py is a Python module and program that checks the format and content of Allow rows of the CSV file to have fewer columns than in the column headers. is not required, if it is present and its data fails some other test, an error will be� Any quick way to check if any row in CSV file contains a specific value? Ask your PowerShell-related questions, including questions on cmdlet development! Forum rules

Comments
  • fail 'No email in input data' unless mail
  • Ok, but it will fail when single record has no email.
  • You claimed you want it to "should break a loop". To simply skip a record, use next if mail.nil?
  • Maybe I have described it wrong. I would like to break loop or don't even start it if there is no column named - "email", "Email" etc. My mail variable is just single email if it will find record in defined colums, but if there is no column it will be always nil.
  • Thank you, that's what I was looking for. I have observed that if you put it inside the loop it will drastically slow down importing. I have used your idea and created my own to skip implementation inside the loop. I will update my post.