Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

python - How to dump the data from file to an excel sheet

I want to dump [3-4 lines together] some data to an excel sheet.
I could able to dump single line based on some criteria [like if line is getting start with // or /* ], but in case of when lines starts from /* and after 3-4 sentences its end with * / .
Only first line which is started from /* and last line which is ended with */ could able to dump.
I'm unable to handle this situation, please help.

Below is my code:-

fileopen = open("test.c")         
for var in fileopen:   
if var.startswith("//"):    
   var1 = var1 + var  
   continue  
 if var.startswith("/*"):  
   var1 = var1 + var  
   continue    

 else:  
   continue  
worksheet.write(i, 5,var1,cell_format)

Note:- Above code will be having indentation issue. As i don't how to put the code properly in stack over flow, so please ignore this issue.

For example:-
/* Test that the correct data prefetch instructions are generated for i386
variants that use 3DNow! prefetchw or SSE prefetch instructions with
locality hints. */

I want to dump entire data at once through python script but i could able to dump only "First Line", which is started with /*.

Any suggestion please!!!
Thanks in Advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
import re

fileopen = open("test.c")

# Convert file to a string
source_code = ""
for var in fileopen:
    source_code += var

# Find the first comment from the source code
pattern = r'//.*?$|/*.*?*/|'(?:\.|[^\'])*'|"(?:\.|[^"])*"'
var1 = re.search(pattern, source_code, re.DOTALL | re.MULTILINE).group() # first comment

var1 = unicode(var1, errors='ignore')
worksheet.write(i, 5, var1, cell_format)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...