如何通过python脚本将数据从文件转储到excel工作表?

2024-03-28 22:22:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将[3-4行]一些数据转储到excel工作表中。
我可以根据一些标准来转储单行[例如,如果行以//或/*]开头,但如果行以/*开头,在3-4句话之后,则以*/结尾。
只有从/*开始的第一行和以*/结束的最后一行可以转储。
我不能处理这种情况,请帮忙。你知道吗

下面是我的代码:-你知道吗

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)

你知道吗注:以上代码将出现缩进问题。因为我不知道如何将代码正确地放入堆栈溢出,所以请忽略这个问题。你知道吗

为了示例:-
/*测试是否为i386生成了正确的数据预取指令
使用3DNow的变体!带
位置提示。*/你知道吗

我想通过python脚本一次转储整个数据,但只能转储以/*开头的“第一行”。你知道吗

任何建议请!!!
提前谢谢。你知道吗


Tags: 数据代码test标准ifvar结尾情况
1条回答
网友
1楼 · 发布于 2024-03-28 22:22:56
import re

fileopen = open("test.c")

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

# Find all the comments from the source code
pattern = r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"'
found = re.findall(pattern, source_code, re.DOTALL | re.MULTILINE) # list of comments

var1 = ""
for var in found:
    var1 = var1 + var

worksheet.write(i, 5,var1,cell_format)

相关问题 更多 >