User Guide Cancel

CSVRead

 

Description

Reads a CSV file into an ordered struct.

History

  • ColdFusion (2025 release): Added the function. 

Syntax

CSVRead(source,outputFormat,rowFilter [, csvFormatConfiguration])
CSVRead(source,outputFormat,rowFilter [, csvFormatConfiguration])
CSVRead(source,outputFormat,rowFilter [, csvFormatConfiguration]) 

Parameters

Name

 

 

Required

 

 

Type

 

 

Description

 

 

source

 

 

Yes

 

 

String 

 

 

Path of file where the csv needs to be read.

 

  • outputFormat

 

 

 

 

Yes

 

 

 

 

String 

 

 

Type of object to be read from the input CSV. The possible values are:
  • csvstring
  • arrayOfCFArray
  • arrayofjavaarray
  • arrayofstruct
  • query
  • xlsx
  • csvrecorditerable

rowFilter

 

 

No

 

 

UDF

 

 

A UDF, which accepts two parameters row array and row number; it returns true or false, which represents whether to read the row or not.  

csvFormatConfiguration

 

 

No

 

 

Struct

 

 

A struct containing the configuration for writing csv files information.  

 

The following values are allowed in the struct:

Key

 

 

Value

 

 

allowMissingColumnNames

 

 

True to allow missing column names in the header line. The default is True. 

 

 

commentMarker

 

 

A character or sequence of characters used to indicate that a line should be treated as a comment and ignored during processing.

 

 

delimiter

 

 

Set the character delimiter. 

 

 

 

 

duplicateHeaderMode

 

 

Set the duplicate header names behavior. The values are:   

  • ALLOW_ALL
  • ALLOW_EMPTY
  • DISALLOW

 

 

 

encoding 

 

 

Encoding to be used for the values in the CSV. The default is UTF-8. The following encoding are supported:
  • utf -8 
  • iso-8859-1
  • windows-1252 
  • us- ascii
  • shift_jis 
  • iso-2022-jp
  • euc -jp 
  • euc - kr
  • big5
  • euc - cn
  • utf -16  

escapeCharacter

 

 

Sets the escape character. 

 

 

headerComments

 

 

Set the header comments set to the given values. The comments will be printed first, before the headers. 

 

 

header

 

 

Set the row values, which will be the header.  

 

 

ignoreEmptyLines

 

 

True, if you want to ignore the empty lines between the records. False, if you want to translate the empty lines to empty records. The default is True. 

 

 

ignoreHeaderCase

 

 

Set the parser case mapping behavior. True if you want to access name/values. False, if you do not want any change. The default is True. 

 

 

 

 

nullString

 

 

 

 

In CSVRead, nullString replaces specified string to NULL values. 

 

 

quoteCharacter

 

 

Set the quote character.  

 

 

 

 

quoteMode

 

 

Set the quote mode. The values are:   

  • ALL_NON_NULL

  • NON_NUMERIC  

  • NONE

recordSeparator 

 

 

Character to separate the CSV records. For example, comma, /n, etc.

 

 

skipHeaderRecord 

 

 

Ignore the specified number of records/rows at the start of the file. The value must be a Boolean.

 

 

trailingDelimiter 

 

 

True, if you want to add a trailing delimiter. The default is True.

 

 

trim 

 

 

True, if you want to trim leading and trailing blanks. The default is True.

 

 

The following matrix lists the support for various output formats:

parameters

 

 

ArrayofCFArray

 

 

ArrayofJavaArray

 

 

xlsx

 

 

csvrecorditable

 

 

csvString

 

 

ArrayOfStruct

 

 

query

 

 

allowMissingColumnNames = false

 

 

NA 

 

 

NA

 

 

NA 

 

 

NA

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

allowMissingColumnNames = true

 

 

NA 

 

 

NA

 

 

NA 

 

 

NA

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

CommentMarker

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

delimiter

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

duplicateHeaderMode = ALLOW_ALL

 

 

NA 

 

 

NA

 

 

NA 

 

 

NA

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

duplicateHeaderMode = ALLOW_EMPTY

 

 

NA 

 

 

NA

 

 

NA 

 

 

NA

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

duplicateHeaderMode = DISALLOW

 

 

NA 

 

 

NA

 

 

NA 

 

 

NA

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

escapeCharacter

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

NA 

 

 

Yes

 

 

Yes 

 

 

header

 

 

NA 

 

 

NA

 

 

NA 

 

 

NA

 

 

NA 

 

 

Yes

 

 

Yes 

 

 

ignoreEmptyLines=false

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

ignoreEmptyLines=true

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

ignoreHeaderCase=false

 

 

NA 

 

 

NA

 

 

NA 

 

 

NA

 

 

NA 

 

 

Yes

 

 

NA 

 

 

ignoreHeaderCase=true

 

 

NA 

 

 

NA

 

 

NA 

 

 

NA

 

 

NA 

 

 

Yes

 

 

NA 

 

 

nullString

 

 

Yes 

 

 

Yes

 

 

NA 

 

 

Yes

 

 

NA 

 

 

Yes

 

 

Yes 

 

 

quoteCharacter

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

recordSeparator

 

 

NA 

 

 

NA

 

 

NA 

 

 

NA

 

 

Yes 

 

 

NA

 

 

NA 

 

 

skipHeaderRecord=false

 

 

NA 

 

 

NA

 

 

NA 

 

 

NA

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

skipHeaderRecord=true

 

 

NA 

 

 

NA

 

 

NA 

 

 

NA

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

trailingDelimiter=false

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

trailingDelimiter=true

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

Yes

 

 

Yes 

 

 

trim

 

 

NA 

 

 

NA

 

 

NA 

 

 

NA

 

 

Yes 

 

 

NA

 

 

NA 

 

 

Example 1

Reading from a CSV as csvstring.

<cfscript>
theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv";
format="csvstring"
csvObj = csvread(filepath = theFile, outputformat = format)
writeDump(csvObj)
</cfscript>
<cfscript> theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; format="csvstring" csvObj = csvread(filepath = theFile, outputformat = format) writeDump(csvObj) </cfscript>
<cfscript> 
    theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; 
    format="csvstring" 
    csvObj = csvread(filepath = theFile, outputformat = format) 
    writeDump(csvObj) 
</cfscript> 

Output- partial

Index,Customer Id,First Name,Last Name,Company,City,Country,Phone 1,Phone 2,Email,Subscription Date,Website 1,DD37Cf93aecA6Dc,Sheryl,Baxter,Rasmussen Group,East Leonard,Chile,229.077.5154,397.884.0519x718,zunigavanessa@smith.info,2020-08-24,http://www.stephenson.com/ 2,1Ef7b82A4CAAD10,Preston,Lozano,Vega-Gentry,East Jimmychester,Djibouti,5153435776,686-620-1820x944,vmata@colon.com,2021-04-23,http://www.hobbs.com/ 3,6F94879bDAfE5a6,Roy,Berry,Murillo-Perry,Isabelborough,Antigua and Barbuda,+1-539-402-0259,(496)978-3969x58947,beckycarr@hogan.com,2020-03-25,http://www.lawrence.com/ 4,5Cef8BFA16c5e3c,Linda,Olsen,"Dominguez, Mcmillan and Donovan",Bensonview,Dominican Republic,001-808-617-6467x12895,+1-813-324-8756,stanleyblackwell@benson.org,2020-06-02,http://www.good-lyons.com/ 5,053d585Ab6b3159,Joanna,Bender,"Martin, Lang and Andrade",West Priscilla,Slovakia (Slovak Republic),001-234-203-0635x76146,001-199-446-3860x3486,colinalvarado@miles.net,2021-04-17,https://goodwin-ingram.com/ 6,2d08FB17EE273F4,Aimee,Downs,Steele Group,Chavezborough,Bosnia and Herzegovina,(283)437-3886x88321,999-728-1637,louis27@gilbert.com,2020-02-25,http://www.berger.net/ 7,EA4d384DfDbBf77,Darren,Peck,"Lester, Woodard and Mitchell",Lake Ana,Pitcairn Islands,(496)452-6181x3291,+1-247-266-0963x4995,tgates@cantrell.com,2021-08-24,https://www.le.com/ 8,0e04AFde9f225dE,Brett,Mullen,"Sanford, Davenport and Giles",Kimport,Bulgaria,001-583-352-7197x297,001-333-145-0369,asnow@colon.com,2021-04-12,https://hammond-ramsey.com/ 9,C2dE4dEEc489ae0,Sheryl,Meyers,Browning-Simon,Robersonstad,Cyprus,854-138-4911x5772,+1-448-910-2276x729,mariokhan@ryan-pope.org,2020-01-13,https://www.bullock.net/ 10,8C2811a503C7c5a,Michelle,Gallagher,Beck-Hendrix,Elaineberg,Timor-Leste,739.218.2516x459,001-054-401-0347x617,mdyer@escobar.net,2021-11-08,https://arias.com/ 11,216E205d6eBb815,Carl,Schroeder,"Oconnell, Meza and Everett",Shannonville,Guernsey,637-854-0256x825,114.336.0784x788,kirksalas@webb.com,2021-10-20,https://simmons-hurley.com/ 12,CEDec94deE6d69B,Jenna,Dodson,"Hoffman, Reed and Mcclain",East Andrea,Vietnam,(041)737-3846,+1-556-888-3485x42608,mark42@robbins.com,2020-11-29,http://www.douglas.net/ 13,e35426EbDEceaFF,Tracey,Mata,Graham-Francis,South Joannamouth,Togo,001-949-844-8787,(855)713-8773,alex56@walls.org,2021-12-02,http://www.beck.com/ 14,A08A8aF8BE9FaD4,Kristine,Cox,Carpenter-Cook,Jodyberg,Sri Lanka,786-284-3358x62152,+1-315-627-1796x8074,holdenmiranda@clarke.com,2021-02-08,https://www.brandt.com/ 15,6fEaA1b7cab7B6C,Faith,Lutz,Carter-
Index,Customer Id,First Name,Last Name,Company,City,Country,Phone 1,Phone 2,Email,Subscription Date,Website 1,DD37Cf93aecA6Dc,Sheryl,Baxter,Rasmussen Group,East Leonard,Chile,229.077.5154,397.884.0519x718,zunigavanessa@smith.info,2020-08-24,http://www.stephenson.com/ 2,1Ef7b82A4CAAD10,Preston,Lozano,Vega-Gentry,East Jimmychester,Djibouti,5153435776,686-620-1820x944,vmata@colon.com,2021-04-23,http://www.hobbs.com/ 3,6F94879bDAfE5a6,Roy,Berry,Murillo-Perry,Isabelborough,Antigua and Barbuda,+1-539-402-0259,(496)978-3969x58947,beckycarr@hogan.com,2020-03-25,http://www.lawrence.com/ 4,5Cef8BFA16c5e3c,Linda,Olsen,"Dominguez, Mcmillan and Donovan",Bensonview,Dominican Republic,001-808-617-6467x12895,+1-813-324-8756,stanleyblackwell@benson.org,2020-06-02,http://www.good-lyons.com/ 5,053d585Ab6b3159,Joanna,Bender,"Martin, Lang and Andrade",West Priscilla,Slovakia (Slovak Republic),001-234-203-0635x76146,001-199-446-3860x3486,colinalvarado@miles.net,2021-04-17,https://goodwin-ingram.com/ 6,2d08FB17EE273F4,Aimee,Downs,Steele Group,Chavezborough,Bosnia and Herzegovina,(283)437-3886x88321,999-728-1637,louis27@gilbert.com,2020-02-25,http://www.berger.net/ 7,EA4d384DfDbBf77,Darren,Peck,"Lester, Woodard and Mitchell",Lake Ana,Pitcairn Islands,(496)452-6181x3291,+1-247-266-0963x4995,tgates@cantrell.com,2021-08-24,https://www.le.com/ 8,0e04AFde9f225dE,Brett,Mullen,"Sanford, Davenport and Giles",Kimport,Bulgaria,001-583-352-7197x297,001-333-145-0369,asnow@colon.com,2021-04-12,https://hammond-ramsey.com/ 9,C2dE4dEEc489ae0,Sheryl,Meyers,Browning-Simon,Robersonstad,Cyprus,854-138-4911x5772,+1-448-910-2276x729,mariokhan@ryan-pope.org,2020-01-13,https://www.bullock.net/ 10,8C2811a503C7c5a,Michelle,Gallagher,Beck-Hendrix,Elaineberg,Timor-Leste,739.218.2516x459,001-054-401-0347x617,mdyer@escobar.net,2021-11-08,https://arias.com/ 11,216E205d6eBb815,Carl,Schroeder,"Oconnell, Meza and Everett",Shannonville,Guernsey,637-854-0256x825,114.336.0784x788,kirksalas@webb.com,2021-10-20,https://simmons-hurley.com/ 12,CEDec94deE6d69B,Jenna,Dodson,"Hoffman, Reed and Mcclain",East Andrea,Vietnam,(041)737-3846,+1-556-888-3485x42608,mark42@robbins.com,2020-11-29,http://www.douglas.net/ 13,e35426EbDEceaFF,Tracey,Mata,Graham-Francis,South Joannamouth,Togo,001-949-844-8787,(855)713-8773,alex56@walls.org,2021-12-02,http://www.beck.com/ 14,A08A8aF8BE9FaD4,Kristine,Cox,Carpenter-Cook,Jodyberg,Sri Lanka,786-284-3358x62152,+1-315-627-1796x8074,holdenmiranda@clarke.com,2021-02-08,https://www.brandt.com/ 15,6fEaA1b7cab7B6C,Faith,Lutz,Carter-
Index,Customer Id,First Name,Last Name,Company,City,Country,Phone 1,Phone 2,Email,Subscription Date,Website 1,DD37Cf93aecA6Dc,Sheryl,Baxter,Rasmussen Group,East Leonard,Chile,229.077.5154,397.884.0519x718,zunigavanessa@smith.info,2020-08-24,http://www.stephenson.com/ 2,1Ef7b82A4CAAD10,Preston,Lozano,Vega-Gentry,East Jimmychester,Djibouti,5153435776,686-620-1820x944,vmata@colon.com,2021-04-23,http://www.hobbs.com/ 3,6F94879bDAfE5a6,Roy,Berry,Murillo-Perry,Isabelborough,Antigua and Barbuda,+1-539-402-0259,(496)978-3969x58947,beckycarr@hogan.com,2020-03-25,http://www.lawrence.com/ 4,5Cef8BFA16c5e3c,Linda,Olsen,"Dominguez, Mcmillan and Donovan",Bensonview,Dominican Republic,001-808-617-6467x12895,+1-813-324-8756,stanleyblackwell@benson.org,2020-06-02,http://www.good-lyons.com/ 5,053d585Ab6b3159,Joanna,Bender,"Martin, Lang and Andrade",West Priscilla,Slovakia (Slovak Republic),001-234-203-0635x76146,001-199-446-3860x3486,colinalvarado@miles.net,2021-04-17,https://goodwin-ingram.com/ 6,2d08FB17EE273F4,Aimee,Downs,Steele Group,Chavezborough,Bosnia and Herzegovina,(283)437-3886x88321,999-728-1637,louis27@gilbert.com,2020-02-25,http://www.berger.net/ 7,EA4d384DfDbBf77,Darren,Peck,"Lester, Woodard and Mitchell",Lake Ana,Pitcairn Islands,(496)452-6181x3291,+1-247-266-0963x4995,tgates@cantrell.com,2021-08-24,https://www.le.com/ 8,0e04AFde9f225dE,Brett,Mullen,"Sanford, Davenport and Giles",Kimport,Bulgaria,001-583-352-7197x297,001-333-145-0369,asnow@colon.com,2021-04-12,https://hammond-ramsey.com/ 9,C2dE4dEEc489ae0,Sheryl,Meyers,Browning-Simon,Robersonstad,Cyprus,854-138-4911x5772,+1-448-910-2276x729,mariokhan@ryan-pope.org,2020-01-13,https://www.bullock.net/ 10,8C2811a503C7c5a,Michelle,Gallagher,Beck-Hendrix,Elaineberg,Timor-Leste,739.218.2516x459,001-054-401-0347x617,mdyer@escobar.net,2021-11-08,https://arias.com/ 11,216E205d6eBb815,Carl,Schroeder,"Oconnell, Meza and Everett",Shannonville,Guernsey,637-854-0256x825,114.336.0784x788,kirksalas@webb.com,2021-10-20,https://simmons-hurley.com/ 12,CEDec94deE6d69B,Jenna,Dodson,"Hoffman, Reed and Mcclain",East Andrea,Vietnam,(041)737-3846,+1-556-888-3485x42608,mark42@robbins.com,2020-11-29,http://www.douglas.net/ 13,e35426EbDEceaFF,Tracey,Mata,Graham-Francis,South Joannamouth,Togo,001-949-844-8787,(855)713-8773,alex56@walls.org,2021-12-02,http://www.beck.com/ 14,A08A8aF8BE9FaD4,Kristine,Cox,Carpenter-Cook,Jodyberg,Sri Lanka,786-284-3358x62152,+1-315-627-1796x8074,holdenmiranda@clarke.com,2021-02-08,https://www.brandt.com/ 15,6fEaA1b7cab7B6C,Faith,Lutz,Carter- 

Example 2

Reading from a CSV as array of arrays.

<cfscript>
theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv";
format="arrayofcfarray"
csvObj = csvread(filepath = theFile, outputformat = format)
writeDump(csvObj)
</cfscript>
<cfscript> theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; format="arrayofcfarray" csvObj = csvread(filepath = theFile, outputformat = format) writeDump(csvObj) </cfscript>
<cfscript> 
    theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; 
    format="arrayofcfarray" 
    csvObj = csvread(filepath = theFile, outputformat = format) 
    writeDump(csvObj) 
</cfscript> 

Example 3

Reading from a CSV as XLSX

<cfscript>
theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv";
format="xlsx"
csvObj = csvread(filepath = theFile, outputformat = format)
writeDump(csvObj)
</cfscript>
<cfscript> theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; format="xlsx" csvObj = csvread(filepath = theFile, outputformat = format) writeDump(csvObj) </cfscript>
<cfscript> 
    theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; 
    format="xlsx" 
    csvObj = csvread(filepath = theFile, outputformat = format) 
    writeDump(csvObj) 
</cfscript> 

Example 4

Reading from a CSV as array of structs.

If you want to read values from a CSV as array of structs, you must specify the header of the CSV as an array and pass the array in the configuration struct. 

See the example:

<cfscript>
theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv";
format="arrayofstruct"
headerArray=["Index"," Customer Id", "Name", "Last Name", "Company","City","Country","Phone 1", "Phone 2", "Email", "Subscription Date","Website"]
readconfiguration={
"header"=#headerArray#
}
csvObj = csvread(filepath = theFile, outputformat = format, csvformatconfiguration=readconfiguration)
writeDump(csvObj)
</cfscript>
<cfscript> theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; format="arrayofstruct" headerArray=["Index"," Customer Id", "Name", "Last Name", "Company","City","Country","Phone 1", "Phone 2", "Email", "Subscription Date","Website"] readconfiguration={ "header"=#headerArray# } csvObj = csvread(filepath = theFile, outputformat = format, csvformatconfiguration=readconfiguration) writeDump(csvObj) </cfscript>
<cfscript> 
    theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; 
    format="arrayofstruct" 
    headerArray=["Index"," Customer Id", "Name", "Last Name", "Company","City","Country","Phone 1", "Phone 2", "Email", "Subscription Date","Website"] 
    readconfiguration={ 
        "header"=#headerArray# 
    } 
    csvObj = csvread(filepath = theFile, outputformat = format, csvformatconfiguration=readconfiguration) 
    writeDump(csvObj) 
</cfscript>

Get help faster and easier

New user?