Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
csv_io
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Bohn
csv_io
Commits
be637096
Commit
be637096
authored
5 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Added code to parse CSV file
parent
d20d0b42
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
+72
-0
72 additions, 0 deletions
src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
src/main/resources/csv/demo.csv
+5
-0
5 additions, 0 deletions
src/main/resources/csv/demo.csv
with
77 additions
and
0 deletions
src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
0 → 100644
+
72
−
0
View file @
be637096
/*
* CSV Reader/Writer, copyright (c) 2019 Christopher A. Bohn, bohn@unl.edu.
*/
package
edu.unl.cse.csv_io
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.util.*
;
public
class
CSVReaderWriter
{
// this does not (yet) handle data elements that include commas and/or are surrounded by quotation marks
// use of ClassLoader.getResourceAsStream inspired by https://www.mkyong.com/java/java-read-a-file-from-resources-folder/
// use of Map inspired by Python's csv module
private
static
final
String
DELIMTER
=
","
;
private
static
final
char
BYTE_ORDER_MARK
=
'\ufeff'
;
public
static
Set
<
Map
<
String
,
String
>>
readCSV
(
String
filename
)
{
Set
<
Map
<
String
,
String
>>
csvSet
=
null
;
ClassLoader
classLoader
=
CSVReaderWriter
.
class
.
getClassLoader
();
try
(
InputStream
inputStream
=
classLoader
.
getResourceAsStream
(
"csv/"
+
filename
))
{
csvSet
=
parseCSV
(
inputStream
);
}
catch
(
IOException
ioException
)
{
System
.
err
.
println
(
"Error loading "
+
filename
+
". "
+
ioException
);
}
return
csvSet
;
}
private
static
Set
<
Map
<
String
,
String
>>
parseCSV
(
InputStream
inputStream
)
throws
IOException
{
Set
<
Map
<
String
,
String
>>
csvSet
=
new
HashSet
<>();
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
));
String
line
;
List
<
String
>
fieldNames
=
new
ArrayList
<>();
boolean
header
=
true
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
String
[]
elements
=
line
.
split
(
DELIMTER
);
if
(
header
)
{
if
(
elements
[
0
].
charAt
(
0
)
==
BYTE_ORDER_MARK
)
{
elements
[
0
]
=
elements
[
0
].
substring
(
1
);
// remove text-encoding character sometimes added by Excel
}
fieldNames
.
addAll
(
Arrays
.
asList
(
elements
));
header
=
false
;
}
else
{
Map
<
String
,
String
>
row
=
new
HashMap
<>();
int
column
=
0
;
for
(
String
element:
elements
)
{
row
.
put
(
fieldNames
.
get
(
column
++),
element
);
}
csvSet
.
add
(
row
);
}
}
return
csvSet
;
}
public
static
void
main
(
String
[]
args
)
{
Set
<
Map
<
String
,
String
>>
demo
=
readCSV
(
"demo.csv"
);
Set
<
String
>
fieldNames
=
null
;
for
(
Map
<
String
,
String
>
row:
demo
)
{
if
(
fieldNames
==
null
)
{
fieldNames
=
row
.
keySet
();
}
for
(
String
field:
fieldNames
)
{
String
value
=
row
.
get
(
field
);
System
.
out
.
print
(
field
+
":"
+
value
+
"\t"
);
}
System
.
out
.
println
();
}
}
}
This diff is collapsed.
Click to expand it.
src/main/resources/csv/demo.csv
0 → 100644
+
5
−
0
View file @
be637096
A, B, C, D
one,two,,three
four,,five,six
7,8,9,
,alpha,beta,gamma
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment