通过python将文本文件转换为Excel / CSV文件

时间:2019-05-07 12:43:42

标签: python-3.x

我必须读取文本文件,然后选择包含某些特定字符串的行并将这些行写入新的Excel文件,但事实是该行包含许多由空格和其他字符分隔的字符串。所以我想在输出文件的不同列中输出每个字符串。所以基本上我想在输出文件中定界。

matchedline =''
stringtomatch = "UtranCell="
stringtomatch1 = "GsmRelation"
stringtomatch2 = "UtranRelation"
stringtomatch3 = "Hsdsch=1"
stringtomatch4 = "HsAvgNoUsers PSEulTputTot PSEulTputUser PSHsTputTot PSHsTputUser"
stringtomatch5 = "/usr/bin/perl"
with open("RNCTCSA.log") as file1:
    with open(r"C:\Users\vikramku\Desktop\Python\output.csv", "w") as myfile1:
     for line in file1:
        if (stringtomatch in line and stringtomatch1 not in line and stringtomatch2 not in line and stringtomatch3 not in line and stringtomatch5 not in line )or (stringtomatch4 in line):
            matchedline = line
            # print(matchedline,end ='')
            myfile1.write(matchedline)

我只在一列中得到所有这样的输出:

  

RNCTCSA.log:UtranCell = NARU11 pmCellDowntimeAuto 0
  RNCTCSA.log:UtranCell = NARU11 pmCellDowntimeMan 0
  RNCTCSA.log:UtranCell = NARU11 pmDlTrafficVolumePsIntHs 2037
  RNCTCSA.log:UtranCell = NARU11 pmNoDirRetryAtt 0

但是我想要这种格式,以便每个字符串在不同的列中:

enter image description here

0 个答案:

没有答案