简体   繁体   中英

python 3.8 how to convert txt to csv with seperator of 2 or more spaces

I'm trying to convert a txt-export from a telnet session with Python. The txt looks like this

  LocalPort  ChassisId                 PortId PortDescr SysName               

  --------- + ------------------------- ------ --------- ----------------------

  3          01 23 45 67 89 76           1      1         name1

  19         01 23 45 67 89 76           19     19        name2

  21         01 23 45 67 89 76            9      9        name3

  22         01 23 45 67 89 76           22     22        name4

  22         01 23 45 67 89 76           LAN-1  example   name5

  23         01 23 45 67 89 76           LAN-1  example   name6

  23         01 23 45 67 89 76           23     23        name7

So I tried something like that with python:

read_file = pd.read_csv(OUTPUT_FILE_PROCESSED_TXT, sep="\s{2,}", header=5)
read_file.to_csv(OUTPUT_CSV, sep=",", header=["LocalPort","ChassisId","PortId","PortDescr","SysName"])

But the result is that I'm missing a big part between LocalPort 3 and LocalPort 22 in the generated csv. Those lines are removed. So my csv is only the headers and the last three lines.

Can anyone help me and maybe explain why Python is removing some lines? I know some others asked a similar question but the solutions didn't help me

Thanks in advance...

You can parse that OUTPUT_FILE_PROCESSED_TXT file manually:

import re

with open(OUTPUT_FILE_PROCESSED_TXT, "r") as f:
    telnet_output = f.read()

rows = telnet_output.splitlines()
values = [re.split(r"\s{2,}", row.strip())  \  # split when >2 spaces
          for row in rows[1:]  \  # avoid header row
          if not row.strip().startswith("-") and row]  # avoid empty and --- rows

columns = rows[0].split()

df = pd.DataFrame(values, columns=columns)

Check that your df is well defined:

>>> print(df)

  LocalPort          ChassisId PortId PortDescr SysName
0         3  01 23 45 67 89 76      1         1   name1
1        19  01 23 45 67 89 76     19        19   name2
2        21  01 23 45 67 89 76      9         9   name3
3        22  01 23 45 67 89 76     22        22   name4
4        22  01 23 45 67 89 76  LAN-1   example   name5
5        23  01 23 45 67 89 76  LAN-1   example   name6
6        23  01 23 45 67 89 76     23        23   name7

And now export to csv with what you suggested:

df.to_csv(OUTPUT_CSV, sep=",")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM