Appending data to an excel sheet by python code

M

Manoj Rout

hi
i want to append some data to an existing excel sheet by reading from file.But here i am facing 2 problem.first is when i am reading from file.


import re

file=open("C:\Workspace\WS2\P1\Dave\Generated\src\UART001\UART001_Conf.c","r")
strings=re.search(r'(?<=(.Mode = UART_))\w+',file.read())
string=re.search(r'(?<=(.StopBit = UART_))\w+',file.read())

print strings.group()
print string.group()

workbook.close()



Here if i will read only once then it is fine and not showing any error.But if i will do it for second time i.e.2nd string i have to search and print both of them,then it is showing error.

And one more problem how to append some data to an existing excel sheet by python code.

so can u please help me on this problem.

Thanks and with regards
Manoj Rout
 
M

MRAB

hi
i want to append some data to an existing excel sheet by reading from file.But here i am facing 2 problem.first is when i am reading from file.


import re
It's a good idea to use raw string literals for file paths on
Windows. Alternatively, you could use forward slashes instead of
backslashes.
file=open("C:\Workspace\WS2\P1\Dave\Generated\src\UART001\UART001_Conf.c","r")

Here you're reading the entire file:
strings=re.search(r'(?<=(.Mode = UART_))\w+',file.read())

Here you're trying to read the entire file again, but you're already
read to its end, so you'll now get an empty string:
string=re.search(r'(?<=(.StopBit = UART_))\w+',file.read())

print strings.group()
print string.group()
What's 'workbook'? Don't you mean 'file'?
workbook.close()



Here if i will read only once then it is fine and not showing any error.But if i will do it for second time i.e.2nd string i have to search and print both of them,then it is showing error.
You haven't shown the traceback, so I have to guess what the error was.
 
D

Dennis Lee Bieber

hi
i want to append some data to an existing excel sheet by reading from file.But here i am facing 2 problem.first is when i am reading from file.


import re

file=open("C:\Workspace\WS2\P1\Dave\Generated\src\UART001\UART001_Conf.c","r")

For safety, it is better to work with / rather than \ (the only time
you must have the \ in Windows is when sending the string to a command
shell -- os.system and similar functions)
strings=re.search(r'(?<=(.Mode = UART_))\w+',file.read())

You've just read the entire file and are now at EOF
string=re.search(r'(?<=(.StopBit = UART_))\w+',file.read())
.... so there is nothing left to read here.

Either read the file into memory first and pass the string to both re
calls, or rewind the file (which may require closing/opening since you
specified "r" mode) between the re calls
And one more problem how to append some data to an existing excel sheet by python code.

Your best chances are to use libraries designed to access Excel files
(consider, Office XP Excel files are some crytic binary, while Office 2010
Excel files are zipped XML -- your code won't work on both forms).

If you are on a Windows box, you could use ctypes (or maybe the win32
packages) to access the Excel DLL directly, and navigate just like you
would in an Excel "macro" (visual basic for applications).
 
P

petmertens

Did you consider using xlrd and xlwt?
I guess these package provide a solution to your problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top