【发布时间】:2020-08-02 07:23:01
【问题描述】:
嘿,我得到了一个如下所示的列表:
['192.168.0.1 switch1 ff:ff:ff:ff:ff:ff 657 dynamic port:42','10.60.37.2 switch1 ff:ff:ff:ff:ff:ff 657 dynamic port:41','192.168.0.240 switch1 ff:ff:ff:ff:ff:ff 657 dynamic port:40']
当我导出到 Excell 时,每个块都在不同的行中,这很好,但都在同一个 collunm 上,我希望在第 1 列切换第 2 列等中有地址
excel colunm 1
excel row1: 192.168.0.1 switch1 ff:ff:ff:ff:ff:ff 657 dynamic port:42
excel row2: 10.60.37.2 switch1 ff:ff:ff:ff:ff:ff 657 dynamic port:41
excel row3 :192.168.0.240 switch1 ff:ff:ff:ff:ff:ff 657 dynamic port:40
所以我想要这样的
exlcol1 exlcol2 exl.col3 exl.col4 exl.col5 exl.col6
exlrow1: 192.168.0.1 switch1 ff:ff:ff:ff:ff:ff 657 dynamic port:42
exlrow2: 10.60.37.2 switch1 ff:ff:ff:ff:ff:ff 657 dynamic port:41
exlrow3 :192.168.0.240 switch1 ff:ff:ff:ff:ff:ff 657 dynamic port:40
我试图将其导出为 csv,但它说 wtr.writerows(row) 必须是 byte.object 之类的,然后我尝试将我的列表更改为字符串以将其更改为 byte.object 但它说我的 wtr.writerows(row) 应该是可交互的。
这是我用来导出到 csv 的代码
import csv
with open('test.csv', 'wb') as f:
wtr = csv.writer(f, delimiter= ',')
for row in data11parts1:
wtr.writerows(row)
with open('test.csv', 'r') as f:
for line in f:
print (line)
我的偏好是将其导出到 Excel,但我认为这可以帮助您更好地解释我的问题和我拥有的数据类型。 只是告诉你我从这个附加中得到了我的 data11parts1:
for element in data2:
elementstring=''.join(element)
for element in res1:
elementstring5=''.join(element)
if elementstring in elementstring5:
with open(Sw1, 'r') as f3:
for line5 in f3:
if elementstring in line5:
print('managemnet-tool' + ' - ' + Sw1.rsplit('.txt',1)[0] + ' - ' + elementstring5.rsplit('-', 1)[0] + ' - ' + line5.rsplit('\n', 1)[0])
data11.append(elementstring5.rsplit('-',1)[0] + ' ' + Sw1.rsplit('.txt',1)[0] + ' ' + line5.rsplit('\n', 1)[0])
data13.append(str(line5.rsplit('\n', 1)[0]))
exit
【问题讨论】:
-
wtr.writerows(row) TypeError: a bytes-like object is required, not 'str' 它给了我这个错误。
-
更改为
open('test.csv', 'w')