Skip to content
Snippets Groups Projects
Commit 28f540b7 authored by rps52's avatar rps52
Browse files

assn01

parents
Branches master
No related tags found
No related merge requests found
File added
File added
#!/usr/bin/python3
class csvCReader:
def __init__(self, path):
self.path = path
self.matrix = self.rCSV()
#Reads in a csv file and puts it in a 2d array
def rCSV(self):
matrix = []
with open(self.path) as f:
for line in f:
line = line.split(',')
matrix.append(line)
f.closed
return matrix
#prints out a formatted table of the csv file
def printCSV(self):
self.csvInteg()
rowLength = len(self.matrix[0])
for i in range(len(self.matrix)):
for x in range(rowLength):
print('|{:^12}'.format(self.matrix[i][x]), end =' ')
print('\n')
for y in range(rowLength * 13):
print("-", end ='')
print('\n')
#Checks to make sure a file contains the same amount of columns in each row
def csvInteg(self):
for i in range(len(self.matrix)):
if len(self.matrix[0]) != len(self.matrix[i]):
raise ValueError('Invalid CSV file')
'''
def printCol(path, col):
matrix = rCSV(path)
print("Printing column {}".format(col))
for i in range(len(matrix)):
print(matrix[i][col - 1])
def printRow(path, row):
matrix = rCSV(path)
rowLength = len(matrix[0])
print("Printing row {}".format(row))
for i in range(rowLength):
print('|{0:10}'.format(matrix[row - 1][i]), end=' ')
print('\n')
'''
#!/usr/bin/python3
Class csvReader:
def __init__(self, path):
self.path
self.matrix = []
#Reads in a csv file and puts it in a 2d array
def rCSV(self, path):
with open(path) as f:
matrix = []
for line in f:
line = line.split(',')
self.matrix.append(line)
f.closed
# return matrix
#Checks to make sure a file contains the same amount of columns in each row
def csvInteg(matrix):
for i in range(len(matrix)):
if len(matrix[0]) != len(matrix[i]):
raise ValueError('Invalid CSV file')
#prints out a formatted table of the csv file
def printCSV(path):
matrix = rCSV(path)
rowLength = len(matrix[0])
for i in range(len(matrix)):
for x in range(rowLength):
print('|{:^12}'.format(matrix[i][x]), end =' ')
print('\n')
for y in range(rowLength * 13):
print("-", end ='')
print('\n')
def printCol(path, col):
matrix = rCSV(path)
print("Printing column {}".format(col))
for i in range(len(matrix)):
print(matrix[i][col - 1])
def printRow(path, row):
matrix = rCSV(path)
rowLength = len(matrix[0])
print("Printing row {}".format(row))
for i in range(rowLength):
print('|{0:10}'.format(matrix[row - 1][i]), end=' ')
print('\n')
/* Copyright (C) 1999 Lucent Technologies */
/* Excerpted from 'The Practice of Programming' */
/* by Brian W. Kernighan and Rob Pike */
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
class Csv { // read and parse comma-separated values
// sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625
public:
Csv( istream& fin=cin, string sep = "," ) :
fin(fin), fieldsep(sep) {}
int getline(string&);
string getfield(int n);
int getnfield() const { return nfield; }
private:
istream& fin; // input file pointer
string line; // input line
vector<string> field; // field strings
int nfield; // number of fields
string fieldsep; // separator characters
int split();
int endofline(char);
int advplain(const string& line, string& fld, int);
int advquoted(const string& line, string& fld, int);
};
// endofline: check for and consume \r, \n, \r\n, or EOF
int Csv::endofline(char c)
{
int eol;
eol = (c=='\r' || c=='\n');
if (c == '\r') {
fin.get(c);
if (!fin.eof() && c != '\n')
fin.putback(c); // read too far
}
return eol;
}
// getline: get one line, grow as needed
int Csv::getline(string& str)
{
char c;
for (line = ""; fin.get(c) && !endofline(c); )
line += c;
split();
str = line;
return !fin.eof();
}
// split: split line into fields
int Csv::split()
{
string fld;
int i, j;
nfield = 0;
if (line.length() == 0)
return 0;
i = 0;
do {
if (i < line.length() && line[i] == '"')
j = advquoted(line, fld, ++i); // skip quote
else
j = advplain(line, fld, i);
if (nfield >= field.size())
field.push_back(fld);
else
field[nfield] = fld;
nfield++;
i = j + 1;
} while (j < line.length());
return nfield;
}
// advquoted: quoted field; return index of next separator
int Csv::advquoted(const string& s, string& fld, int i)
{
int j;
fld = "";
for (j = i; j < s.length(); j++) {
if (s[j] == '"' && s[++j] != '"') {
int k = s.find_first_of(fieldsep, j);
if (k > s.length()) // no separator found
k = s.length();
for (k -= j; k-- > 0; )
fld += s[j++];
break;
}
fld += s[j];
}
return j;
}
// advplain: unquoted field; return index of next separator
int Csv::advplain(const string& s, string& fld, int i)
{
int j;
j = s.find_first_of(fieldsep, i); // look for separator
if (j > s.length()) // none found
j = s.length();
fld = string(s, i, j-i);
return j;
}
// getfield: return n-th field
string Csv::getfield(int n)
{
if (n < 0 || n >= nfield)
return "";
else
return field[n];
}
// Csvtest main: test Csv class
int main(void)
{
string line;
Csv csv;
//Csv csv2( ifstream( "harder.csv" ));
while (csv.getline(line) != 0) {
cout << "line = `" << line <<"'\n";
for (int i = 0; i < csv.getnfield(); i++)
cout << "field[" << i << "] = `"
<< csv.getfield(i) << "'\n";
}
return 0;
}
,Robert,Smargiassi,test
1,2,3,4
5,6,7,8
1,2,3
4,5,6
7,8
9,10,11
#!/usr/bin/python3
#import csvReader
import csvCReader
path = 'csvtest'
path2 = 'invalcsv'
test = csvCReader.csvCReader(path)
test.printCSV()
'''
csvReader.printCSV(path1)
csvReader.printCol(path, 4)
csvReader.printRow(path, 2)
#csvReader.csvInteg(csvReader.rCSV(path))
#csvReader.csvInteg(csvReader.rCSV(path2))
'''
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment