Reading CSV Files

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
satya369
Posts: 32
Joined: Tue Aug 18, 2015 3:45 am

Reading CSV Files

Post by satya369 » Thu May 26, 2016 1:50 pm

I am trying to read CSV file contents into the memory. Does anyone know how we can do this in the upython. There is no CSV module available in the upython.

Is there any module available for upython that can read Tab Separated Values or text files or CSV files?
The https://github.com/micropython/micropyt ... master/csv is empty

satya369
Posts: 32
Joined: Tue Aug 18, 2015 3:45 am

Re: Reading CSV Files

Post by satya369 » Thu May 26, 2016 6:47 pm

Code: Select all

Data[]
i=0
with open('FileName.csv','r') as file:
	for line in file:
		line_Str=file.readline()
		line_Str=line_Str.rstrip('\n')
		line_Str=line_Str.rstrip('\r')
		Data.append(line_Str.split(','))
I actually did some research and found a way to load the CSV data into any array without using the CSV module.

The above code load each appends each row data into the array after stripping the '\n' and '\r' which are at the end of each line.

To get a value for a particular row and column

Code: Select all

Data[0][1]

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Reading CSV Files

Post by dhylands » Thu May 26, 2016 7:23 pm

That will work as long as you don't have fields which contain commas (and you might not - just thought I'd point that out).

satya369
Posts: 32
Joined: Tue Aug 18, 2015 3:45 am

Re: Reading CSV Files

Post by satya369 » Thu May 26, 2016 8:47 pm

Thank you for pointing out.

Right now I don't have any ",' in my data.

Also this code will work if you have a Tab Separated Value file or any file that has delimiters.

satya369
Posts: 32
Joined: Tue Aug 18, 2015 3:45 am

Re: Reading CSV Files

Post by satya369 » Thu May 26, 2016 9:30 pm

Code: Select all

no_Rows=0 #used to find out the number of rows in the file
with open('fileName.csv','r') as file:
	for line in file:
		line=line.rstrip('\n')
		line=line.rstrip('\r')
		CAN_Parm.append(line.split(','))
		no_Rows+=1
In the previous code, I am reading lines twice. So, the odd no rows are skipped, Above is the correct one.

User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

Re: Reading CSV Files

Post by kamikaze » Wed Sep 07, 2016 1:00 am

is there a reason why there is no csv module?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Reading CSV Files

Post by dhylands » Wed Sep 07, 2016 3:49 am

Nobody has written/ported one yet.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Reading CSV Files

Post by pythoncoder » Wed Sep 07, 2016 7:07 am

A general solution is a little more complex as CSV files can contain quoted strings which can include arbitrary contents including nasties like quotation marks and commas.
Peter Hinch
Index to my micropython libraries.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Reading CSV Files

Post by stijn » Wed Sep 07, 2016 9:22 am

...and the numbers might have been written out using any locale so numbers might contain commas, dots, even spaces (especially the latter is great fun)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Reading CSV Files

Post by pythoncoder » Thu Sep 08, 2016 4:36 am

Indeed. I was lumbered with this some years ago: a simple parser grew like Topsy. Without a definitive spec it was a case of working in the dark and I was never entirely convinced that I'd covered all bases. To get back on topic, a rigorous CSV parser isn't especially 'micro'. It's probably best for people with known, simple CSV subsets to roll their own.
Peter Hinch
Index to my micropython libraries.

Post Reply