Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...