[英]Convert TXT to CSV in Python
我想將其轉換為 csv 文件,其中 header 顯示問題編號,下面的行將是錯誤名稱及其旁邊列中的目錄。
我有一個名為 bucket 的文本文件,如下所示:
- - - - - - -新的 - - - - - - - - - -
第一期
UVM_FATAL:1
1 個測試失敗並出現類似錯誤:
/nfs/disks/ben/rundir/test534
- - - - - - -新的 - - - - - - - - - -
第 2 期
本石:4356175.000 ns 寄存器錯誤
3 個測試失敗並出現類似錯誤:
/nfs/disks/ben/rundir/test234
/nfs/disks/ben/rundir/test321
/nfs/disks/ben/rundir/test123
到目前為止,這是我嘗試過的,但它不起作用。 任何人都知道如何做到這一點?順便說一句,問題不僅限於問題 1 和 2,它可以上升到無窮大取決於“桶”文本文件。
bucket = "/nfs/disks/ben/bucket"
inputfile = open(bucket,"r")
bucketreading = inputfile.readlines()
for presence_of_new in bucketreading:
find_presence_of_new = re.search(r"-------------NEW-------------------",presence_of_new,re.M|re.I)
if presence_of_new != 0:
print "I found NEW!"
for error_name in bucketreading :
find_error_name = re.search(r"UVM_FATAL :",error_name,re.M|re.I)
if find_error_name != 0:
print_output_to_outfile = open(outfile,"w")
print_output_to_outfile.writelines(error_name)
print_output_to_outfile.close()
試試這個代碼。 如果模式匹配,re.search 返回 object,否則返回 None,因此您不需要與 0 進行比較。
bucket = r"/nfs/disks/ben/bucket"
inputfile = open(bucket, "r")
bucketreading = inputfile.readlines()
for presence_of_new in bucketreading:
find_presence_of_new = re.search(r"-------------NEW-------------------", presence_of_new, re.M | re.I)
if find_presence_of_new:
print("I found NEW!")
for error_name in bucketreading:
find_error_name = re.search(r"UVM_FATAL :", error_name, re.M | re.I)
if find_error_name:
print_output_to_outfile = open(outfile, "w")
print_output_to_outfile.writelines(error_name)
print_output_to_outfile.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.