2017-08-03 99 views
0

我有一個txt文件,其中顯示:更改文本文件,CSV(多行)

interface 0/1 
no data 
no data 
no data 
no data 
no data 
interface 0/2 
no data 
etc... 

我想有它輸出到一個csv用的格式:

interface 0/1 | no data | no data | no data | no data | no data 
interface 0/2 | no data | no data | no data | no data | no data 

我已經嘗試使用csv模塊並寫入沒有好結果的行。任何幫助,將不勝感激。

+1

您能否提供一個[最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve)並描述您到目前爲止所嘗試的內容? – Constructor

+0

你確定你想要這種格式的輸出,因爲它是非標準的多字符分隔符如''| '價值觀之間? – martineau

回答

0

我已經使用CSV模塊,並寫出沒有很好的效果排嘗試。 任何幫助,將不勝感激。

你在正確的軌道上。您需要csv.writerow(s),但重要的是您如何將數據傳遞給它。

步驟1

遍歷文件和集合行:

rows = [] 
for line in open('file.txt'): 
    if 'interface' in line: 
     rows.append([]) 
    rows[-1].append(line) 

步驟2

寫入行到csv

import csv 
with open('file.csv', 'w') as f: # you need `wb` on python2 
    writer = csv.writer(f, delimiter='|') 
    f.writerows(rows) 
+1

在Python 3中,您需要使用:open('file.csv','w',newline ='')'而不是''wb'''。 – martineau

+0

此外,'csv.writer'的'delimiter'必須是單個字符(根據文檔)。 – martineau

+0

@martineau好的,修正了這個問題。 TY。 –

0

外貌像你想組6號線成一排,寫那些,如:

import csv 
from itertools import islice 

with open('input.txt') as fin, open('output.csv', 'w') as fout: 
    pipe_out = csv.writer(fout, delimiter='|') 
    rows = iter(lambda: list(islice(fin, 6)), []) 
    pipe_out.writerows(rows) 
0

下面是一個使用Python 2.7版(不使用CSV)

f=open("data.txt", 'r') # original data 
f1=open("output_data.txt", 'w') # this is to write the output 
newline="" 
for line in f: 
    if "interface" not in line: 
     newline=newline + r" | " + line.strip() 
    else: 
     # print newline # check this 
     f1.write(newline + '\n') 
     newline=line.strip() 
# print newline # just a visual check 
f1.write(newline) 
0

你也可以簡單,稍有不同的答案嘗試不使用csv模塊:

f = open("file.txt", "r") 
of = open("file.csv", "w") 

data = f.readlines() 
checkpoint = "" 

for line in data: 
    line = line.strip("\n") 
    if "interface" in line: 
     of.write(checkpoint + line) 
     checkpoint = "\n" 
    else: 
     of.write(" | " + line) 

f.close() 
of.close() 
0

CSV模塊通常不是必需的。這個腳本可以做得很好。

f1 = open('file1.txt', 'r') 
f2 = open('file2.csv', 'w+') 

lines = f1.read() 
for line in lines.split('\n'): 
    if line == "": 
     continue 
    elif line.startswith("interface"): 
     f2.write("\n"+line+",") 
    else: 
     f2.write(line+",") 
0

我已經做了這樣的:

content = '' 
with open('text_file.txt', 'r') as txt_file: 
    content = txt_file.read() 

blocks = content.split('interface') 

csv_content = '' 
for block in blocks: 
    if block != '': 
    csv_content += 'interface %s\n' % ' | '.join(block.splitlines()) 

with open('csv_file.csv', 'w') as csv_file: 
    csv_file.write(csv_content) 
0

它可以從文件中的每一行採集行的數據並將其作爲一個CSV行每當開始'interface'線是遇到:中interface.csv文件之後

import csv 

input_filename = 'interface.txt' 
output_filename = 'interface.csv' 

with open(input_filename) as infile, \ 
    open(output_filename, 'w', newline='') as outfile: 

    writer = csv.writer(outfile, delimiter='|') 
    row = [] 

    for line in (line.rstrip() for line in infile): 
     if not line.startswith('interface'): 
      row.append(line) 
     else: # new group 
      if row: 
       writer.writerow(row) # output last group 
      row = [line] 

    writer.writerow(row) # output last row 

內容:

interface 0/1|no data|no data|no data|no data|no data 
interface 0/2|no data|no data|no data|no data|no data