【问题标题】:How to export a very special list to excell or csv如何将非常特殊的列表导出到 excel 或 csv
【发布时间】: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')

标签: python regex excel csv


【解决方案1】:

它抱怨是因为您打开文件进行二进制写入。 CSV 编写很容易,将您的内部字符串拆分为列表(按空格拆分)并编写它们:

data = ['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']

# split the strings into their columns
dp = [d.split() for d in data]

import csv
with open("data.txt","w", newline="") as f:    # w not wb and supply newline
    writer = csv.writer(f)

    # write em all
    writer.writerows(dp)

with open("data.txt") as f:
    print(f.read())

文件内容:

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

【讨论】:

  • 我应该怎么做才能从第 1 行而不是第 0 行开始,并为每个列添加一些列名,例如 col 1 - Ip 地址等?另一个问题是,在端口 40 上它没问题,但在端口 3 上它去了其他 col 所以,它看起来像 |port:40| ;在端口 3 上; |端口:|3
  • 你在writer.writerows(dp)之前添加了一个标题行:类似于writer.writerow(['IP','What','MAC','whatever','dny?','port'])
  • 你对 ,dynamic,port:,3 前的 3 逗号有什么想法吗?因为它在其他列打印,因为它
  • 'port: 3' 之间可能有一个空格。尝试dp = [d.replace(": ",":").split() for d in data] - 或清理您的输入。现在去睡觉了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-08
  • 1970-01-01
相关资源
最近更新 更多