如何将数据从文件转储到Excel表 [英] How to dump the data from file to an excel sheet

查看:128
本文介绍了如何将数据从文件转储到Excel表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将[3-4行在一起]一些数据转储到excel表。

我可以根据一些条件转储单行[像线路开始于//或/ *],但是如果行从/ *开始,并且在3-4个句子之后,它以* /结尾。

只有从/ *开始的第一行和以*结尾的最后一行/可以转储。

我无法处理这种情况,请帮忙。



以下是我的代码: -

  fileopen = open(test.c)
在fileopen中为var:
如果var.startswith(/ /):
var1 = var1 + var
continue
如果var.startswith(/ *):
var1 = var1 + var
continue

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

注意: - 以上代码将有缩进问题。因为我没有如何将代码正确地放在堆栈中,所以请忽略这个问题。



例如: -

/ *测试为i386生成正确的数据预取指令

使用3DNow的变体! prefetchw或SSE预取指令与

locality提示。 * /



我想通过python脚本一次转储整个数据,但我可以只转储First Line,这是以/*.

任何建议请!!!

感谢提前。

解决方案

  import re 

fileopen = open .c)

#将文件转换为字符串
source_code =
在fileopen中为var:
source_code + = var

#从源代码中找到第一个注释
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)


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.

解决方案

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)

这篇关于如何将数据从文件转储到Excel表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆