vishnu92narayanan
Posts: 34
Joined: Thu Mar 10, 2016 12:26 pm

Accessing Excel sheet using python in Raspbian

Mon Mar 21, 2016 7:16 am

How to store data by sliding between various rows and columns of excel sheet using python? I have used file concept
(fout = open('filename.csv',"+a")
to open the excel file. I need to know how to access the rows and columns of this excel.

User avatar
Grumpy Mike
Posts: 914
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Accessing Excel sheet using python in Raspbian

Mon Mar 21, 2016 9:08 am

Well for a start a .csv file is not an Excel spread sheet file.

It is, as the name implies, as "comma separated value" file.
Print it out and you will see that there are values with commas between them. At the end of a row of values there is a CR character denoting the start of the next row.
To move between columns simply advance to the next value.
To move between the rows, you need to know how many values are on one row, that is between the start and the first CR character. This is the number of values you must skip to get to the value directly underneath the current value in the next row.

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Accessing Excel sheet using python in Raspbian

Mon Mar 21, 2016 9:10 am

If your reading and writing to a csv file I would take a look at python's csv module. Its made for the job.

https://docs.python.org/2/library/csv.html
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

PiGraham
Posts: 3602
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Accessing Excel sheet using python in Raspbian

Mon Mar 21, 2016 9:23 am

If you actually need to access an Excel format spreadsheet file (rather than csv) this might be of interest: https://openpyxl.readthedocs.org/en/2.3.3/

CSV files are simple ASCII text files. They are easy to create or read progressively with standard file I/o functions.

vishnu92narayanan
Posts: 34
Joined: Thu Mar 10, 2016 12:26 pm

Re: Accessing Excel sheet using python in Raspbian

Mon Mar 21, 2016 11:09 am

Thank you for the response. In csv file, I could able to write, but I want the files to be copied column wise , i.e once the number of entries reaches 5 (say 1,2,3,4,5), it should go to next column. How can I do this using python (I couldnt able to install xlrd, nor similar files, hence has only csv option). Thank you in advance

PiGraham
Posts: 3602
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Accessing Excel sheet using python in Raspbian

Mon Mar 21, 2016 11:41 am

vishnu92narayanan wrote:Thank you for the response. In csv file, I could able to write, but I want the files to be copied column wise , i.e once the number of entries reaches 5 (say 1,2,3,4,5), it should go to next column. How can I do this using python (I couldnt able to install xlrd, nor similar files, hence has only csv option). Thank you in advance
Look up the CSV format.

Each comma separates columns. A newline separates rows.

1,2,3,4,5
6,7,8,9,0


That text will result in a spreadsheet with two rows of 5 columns.

1,2,,4,5
6,7,,9,0

That is 5 columns again, but the third column is blank

"Col 1","Col 2",,"Col 4","Col 5"

1,2,,4,5
6,7,,9,0

That produces a four row sheet with heading text in the first row, a blank second row and column three blank.

You can try this out simply by typing text into a text file, saving it with .csv extension and opening it in Excel.

vishnu92narayanan
Posts: 34
Joined: Thu Mar 10, 2016 12:26 pm

Re: Accessing Excel sheet using python in Raspbian

Tue Mar 22, 2016 3:32 am

Thank you so much !! I tried it , but faced a problem.
c=0
while(1):

fout = open('a.csv','a')
c=c+1
if ((c%5)==0):
fout.write(',');
x=ser.readline()
print (x)
fout.write(x)
fout.close()
The problem I faced is, as I am closing and opening the file it goes to a new line in the append mode. The result looks like:
1
2
3
4
(Written in Col II)5
6
7
etc... How can I avoid this.? Moreover if I want to make the file to be printed like starting from next column (i.e the number 5 must be at the same position as the number 1 in the next column) what should be done? Thank you in advance

Return to “Python”