使用Python将txt转换为csv

时间:2015-08-22 05:35:33

标签: python mysql sql csv lxml

我有一个txt格式的sql转储,它看起来像这样 -

"Date:","8/21/2015","","Time:","16:18:38","","Name:","NC.S.RHU10.BRD"
"System Name:","NC.S.RHU10.BRD"
"Operator:","SYSTEM"
"Action:","Trend data loss"
"Comment:"," trend definition data loss occurred at 10:21:05 AM on 8/21/2015"
"Revision:","6"
"Location:",""
"Seq Number:","1278738"
" ********************************************************************************"
"Date:","8/21/2015","","Time:","16:17:17","","Name:","SC.L.SIDESHOWBOB.MBC009"
"System Name:","SC.L.SIDESHOWBOB.MBC009"
"Operator:","SYSTEM"
"Action:","FLN device return from failure"
"Comment:","Z8 RETURN from failure in Cabinet 9, Lan 3, Drop 1."
"Revision:","81"
"Location:","SC.L.SIDESHOWBOB.MBC009"
"Seq Number:","1278737"
" ********************************************************************************"
"Date:","8/21/2015","","Time:","16:17:17","","Name:","NC.S.EHU07.EAT"
"System Name:","NC.S.EHU07.EAT"
"Operator:","ITWVSIEMP01\InsightSCH"
"Action:","Trend data collection The target object could not be found on the Field"
"Panel."
"Comment:","Trend COV (0.000)  Failed - The target object could not be found on the"
"Field Panel"
"Revision:","1318"
"Location:","ITWVSIEMP01"
"Seq Number:","1278735"
" ********************************************************************************"
"Date:","8/21/2015","","Time:","16:17:15","","Name:","NC.S.EHU03.TCFM"
"System Name:","NC.S.EHU03.TCFM"
"Operator:","ITWVSIEMP01\InsightSCH"
"Action:","Trend data collection"
"Comment:","COV                Data Loss Detected"
"Revision:","1481"
"Location:","ITWVSIEMP01"
"Seq Number:","1278734"
" ********************************************************************************

我想使用Python以列方式转换以下字段: -

"Date","Time","Name","System Name","Operator","Action","Comment","Type","Revision","Location","Seq Number"

python中是否有就绪函数可以执行此操作?

4 个答案:

答案 0 :(得分:1)

import csv

c = csv.writer(open('out.csv', 'w'), delimiter=',')

file = open('myfile.txt')
for col in file:
  data = col.split('\t')
 # find index "Date=0","Time=1","Name=2","System Name=3","Operator=4","Action=5","Comment=6","Type=7","Revision=8","Location=9","Seq Number=10"
  c.writerow(data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10])
f.close()

答案 1 :(得分:0)

import operator
import csv

with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
    data = {}
    writer = csv.writer(outfile, delimiter=',')
    writer.writerow(["Date","Time","Name","System Name","Operator","Action","Comment","Revision","Location","Seq Number"])
    fields = operator.itemgetter("Date","Time","Name","System Name","Operator","Action","Comment","Revision","Location","Seq Number")
    for line in infile:
        if line.startswith('" *'):
            try:
                writer.writerow(fields(data))
            except AttributeError:
                print('malformed input')
                raise
            data = {}
            continue

        parts = line.split(',')
        if line.startswith('"Date'):
            data['Date'] = parts[1]
            data['Time'] = parts[4]
            data['Name'] = parts[-1]
            continue

        name = parts[0].strip('"').rstrip(":")
        value = parts[1].strip('"')
        data[name] = value

答案 2 :(得分:0)

我刚刚写了一个小实用程序here。也许这可以帮到你。

我认为输入文件的最后一行缺少"。请在末尾添加它以获得统一的分隔符。

答案 3 :(得分:0)

以下脚本应该有效,它会自动生成标题字段并保留CSV文件中的顺序,因此如果格式有所改变,它仍然可以工作:

import csv

with open("sqldump.txt", "r") as f_input, open("output.csv", "wb") as f_output:
    csv_input = csv.reader(f_input)
    csv_output = csv.writer(f_output)

    headers = []
    for cols in csv_input:
        if len(cols) > 1:
            headers.extend([header.strip(":") for header in cols if header.endswith(':')])
        else:
            break

    csv_output.writerow(headers)
    f_input.seek(0)

    entry = []
    for cols in csv_input:
        if cols[0] == 'Date:':
            entry.extend([cols[1], cols[4], cols[-1]])
        elif len(cols) > 1:
            entry.append(cols[1])
        elif cols[0].startswith(' *'):
            csv_output.writerow(entry)
            entry = []

这会为您提供一个输出CSV文件,如下所示:

Date,Time,Name,System Name,Operator,Action,Comment,Revision,Location,Seq Number
8/21/2015,16:18:38,NC.S.RHU10.BRD,NC.S.RHU10.BRD,SYSTEM,Trend data loss, trend definition data loss occurred at 10:21:05 AM on 8/21/2015,6,,1278738
8/21/2015,16:17:17,SC.L.SIDESHOWBOB.MBC009,SC.L.SIDESHOWBOB.MBC009,SYSTEM,FLN device return from failure,"Z8 RETURN from failure in Cabinet 9, Lan 3, Drop 1.",81,SC.L.SIDESHOWBOB.MBC009,1278737
8/21/2015,16:17:17,NC.S.EHU07.EAT,NC.S.EHU07.EAT,ITWVSIEMP01\InsightSCH,Trend data collection The target object could not be found on the Field,Trend COV (0.000)  Failed - The target object could not be found on the,1318,ITWVSIEMP01,1278735
8/21/2015,16:17:15,NC.S.EHU03.TCFM,NC.S.EHU03.TCFM,ITWVSIEMP01\InsightSCH,Trend data collection,COV                Data Loss Detected,1481,ITWVSIEMP01,1278734

使用Python 2.7进行测试。如果您使用的是Python 3.0,请将代码更改为open("output.csv", "w", newline="")

请注意,示例数据中没有“类型”字段?