将数据放在CSV文件中的相应字段名中使用Python [英] Placing the data in respective field names in a CSV file Using Python

查看:423
本文介绍了将数据放在CSV文件中的相应字段名中使用Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写一个脚本,提取txt文件中的数据并写入CSV文件....我可以拆分数据,但无法将其放置在正确的字段名称......



该文件将如下所示:

  xxxx =qqqqqqq 5466 78455yyyy =wwwwzzzz =hhhhmmmm =aaaa
xxxx =qqqqqqq 8795 32133yyyy =wwwwzzzz =hhhhmmmm =aaaa
xxxx = qqqqqqq 5995 21384zzzz =hhhhmmmm =aaaa
xxxx =qqqqqqq 6546 12346yyyy =wwwwzzzz =hhhhmmmm =aaaa
xxxx =qqqqqqq 7895 13246 yyyy =wwwwzzzz =hhhhmmmm =aaaa
xxxx =qqqqqqq 64654 94343yyyy =wwwwmmmm =aaaa

我使用的代码是:

  import csv 
import re


fileread = str(输入(输入文件名称))
fread = open(fileread,r)

resultFile = open(out3.csv,'w')
wr = csv.writer(resultFile)


try:

for fread中的行:
如果行中有attack:
regexs = re.findall(r'\(。+?)\',lines)
wr.writerow(regexs)

finally:
fread.close()
resultFile.close()

结果如下:

  xxxx yyyy zzzz mmmm 

qqqqqqq 5466 78455 wwww hhhh aaaa
qqqqqqq 8795 32133 wwww hhhh aaaa
qqqqqqq 5995 21384 hhhh aaaa
qqqqqqq 6546 12346 wwww hhhh aaaa
qqqqqqq 7895 13246 wwww但是我需要的输出为:






>

  xxxx yyyy zzzz mmmm 

qqqqqqq 5466 78455 wwww hhhh aaaa
qqqqqqq 8795 32133 wwww hhhh aaaa
qqqqqqq 5995 21384 hhhh aaaa
qqqqqqq 6546 12346 wwww hhhh aaaa
qqqqqqq 7895 13246 wwww hhhh aaaa
qqqqqqq 4654 94343 wwww aaaa

我不知道如何放置它.....
Kindly Pls!帮助我....



感谢提前.......

方案

问题是,你的正则表达式甚至没有尝试匹配字段名称,它只是匹配引号中的一切。



写一个与字段名称匹配的正则表达式,然后您将得到字段名称。例如:

  re.findall(r'(\S +?)\s * = \s * \ (。+?)\',line)

这匹配一组非空格字符后跟任何空格, = ,任何空格和一组引号中的任何内容。由于有两个捕获组,每个输出元素将是一个2元组,例如:

 ('xxxx','qqqqqqq 5466 78455')

所以你可以将2元组列表转换为 dict ,并使用 csv.DictWriter 将字段名称与CSV卷名称相匹配。


Im writing a script that extracts the data in a txt file and write in CSV file....I can able to Split the data but could not able to place it in the correct field name......

The file will be like this:

xxxx = "qqqqqqq  5466 78455" yyyy = "wwww" zzzz = "hhhh" mmmm = "aaaa"
xxxx = "qqqqqqq  8795 32133" yyyy = "wwww" zzzz = "hhhh" mmmm = "aaaa"
xxxx = "qqqqqqq  5995 21384" zzzz = "hhhh" mmmm = "aaaa"
xxxx = "qqqqqqq  6546 12346" yyyy = "wwww" zzzz = "hhhh" mmmm = "aaaa"
xxxx = "qqqqqqq  7895 13246" yyyy = "wwww" zzzz = "hhhh" mmmm = "aaaa"
xxxx = "qqqqqqq 64654 94343" yyyy = "wwww" mmmm = "aaaa

And the code I'm using is:

import csv
import re


fileread = str(input("enter the name of the file :"))
fread = open(fileread, "r")

resultFile = open("out3.csv",'w')
wr = csv.writer(resultFile)


try:

    for lines in fread:
        if "attack" in lines:
            regexs = re.findall(r'\"(.+?)\"',lines)
            wr.writerow(regexs)

finally:
    fread.close()
    resultFile.close()

The result is coming as:

      xxxx             yyyy   zzzz   mmmm

qqqqqqq  5466 78455    wwww   hhhh   aaaa
qqqqqqq  8795 32133    wwww   hhhh   aaaa
qqqqqqq  5995 21384    hhhh   aaaa
qqqqqqq  6546 12346    wwww   hhhh   aaaa
qqqqqqq  7895 13246    wwww   hhhh   aaaa
qqqqqqq  4654 94343    wwww   aaaa

But I needed the output as:

      xxxx             yyyy   zzzz   mmmm

qqqqqqq  5466 78455    wwww   hhhh   aaaa
qqqqqqq  8795 32133    wwww   hhhh   aaaa
qqqqqqq  5995 21384           hhhh   aaaa
qqqqqqq  6546 12346    wwww   hhhh   aaaa
qqqqqqq  7895 13246    wwww   hhhh   aaaa
qqqqqqq  4654 94343    wwww          aaaa

I Don't know how to place it..... Kindly Pls!!! help me in this....

Thanks In Advance.......

解决方案

The problem is that your regular expression doesn't even attempt to match the field names, it just matches everything in quotes.

If you write a regular expression that does match the field names, then you'll get the field names. For example:

re.findall(r'(\S+?)\s*=\s*\"(.+?)\"',line)

This matches a group of non-whitespace characters followed by any whitespace, an =, any whitespace, and a group of anything in quotes. Since there are two capturing groups, each output element will be a 2-tuple, like:

('xxxx', 'qqqqqqq  5466 78455')

So you can just convert the list of 2-tuples into a dict, and use csv.DictWriter to match the field names with the CSV volume names.

这篇关于将数据放在CSV文件中的相应字段名中使用Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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