【发布时间】:2014-01-21 10:00:10
【问题描述】:
首先,感谢您阅读我的帖子。从 .txt 文件解析数据并将其放入 .csv 文件时,我遇到了一个特殊问题。从文本文档中,我尝试从路由器配置文件中绘制主机名和环回地址,然后将其放在 Excel 表中的单独列中。相反,当我添加“outFile.write(”“.join(buffer))”时,它在 excel 表中变得一团糟,它还添加了打印函数没有的字符串。
代码如下:
inFile = open("Data.txt")
outFile = open("result.csv", "w")
buffer = []
keepCurrentSet = True
for line in inFile:
buffer.append(line)
if line.startswith ("hostname"):
print (line)
outFile.write("".join(buffer))
elif line.startswith("interface Loopback"):
print (line)
print (next(inFile))
outFile.write("".join(buffer))
inFile.close()
outFile.close()
这是文本文件
[spoiler]
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname cisco1841
!
boot-start-marker
boot-end-marker
!
logging buffered 51200 warnings
!
no aaa new-model
clock timezone Arizona -7
ip cef
!
!
no ip dhcp use vrf connected
!
ip dhcp excluded-address 10.10.1.1
ip dhcp excluded-address 10.10.3.1
!
ip dhcp pool Inside
network 10.10.1.0 255.255.255.0
dns-server 205.171.3.65 4.2.2.1
default-router 10.10.1.1
!
ip dhcp pool Wireless
import all
network 10.10.3.0 255.255.255.0
dns-server 205.171.3.65 4.2.2.1
default-router 10.10.3.1
lease 3
!
!
multilink bundle-name authenticated
!
!
!
!
username xxxxxxx privilege 15 secret 5 xxxxxxxxxx
!
bridge irb
!
!
!
interface Loopback0
ip address 10.10.0.1 255.255.255.255
!
interface FastEthernet0/0
description Inside LAN
ip address 10.10.1.1 255.255.255.0
ip nat inside
duplex auto
speed auto
[/spoiler]
【问题讨论】:
-
请详细说明您希望输出是什么样的确切!究竟是什么问题?根据您的问题,您的脚本似乎可以工作,但我们无法确定预期的输出!
标签: python python-3.x