gpt4 book ai didi

python - 写入下一列 CSV 模块 Python

转载 作者:太空宇宙 更新时间:2023-11-03 17:15:37 26 4
gpt4 key购买 nike

我已经在寻找答案,我想要的只是它是否看到(如果readline中的description)将数据写入事件单元格所在的下一列,不在下一行。

这是我已经得到的示例:

This is what I want
This is the current data

row = 1

while True:
result = crt.Screen.WaitForStrings( waitStrs )

if result == 2:
break

screenrow = crt.Screen.CurrentRow - 1
readline = crt.Screen.Get(screenrow, 1, screenrow, 80)
items = readline.split(':')

# Split the line ( ":" delimited) and put some fields into Excel
if 'interface ' in readline:
worksheet.writerow(items[:1])
++row
elif ' description' in readline \
or ' ip address' in readline \
or ' port allow' in readline \
or 'binding' in readline:
worksheet.writerow(items[:1]) # <--(write this data in the next column where the active cell is)

示例数据:

Info
interface Aux0/0/1
inferface Vlanif30
description Publc Link to CX600-3-B
ip address 10.132.10.132 255.255.255.252

最佳答案

使用我的代码片段下面给出的示例数据应该可以完成这项工作。如需进一步说明,请参阅代码中的注释。 Python的csv模块的文档可以找到here .

示例数据:

Info
interface Aux0/0/1
inferface Vlanif30
description Public Link to CX600-3-B
ip address 10.132.10.132 255.255.255.252
<小时/>

代码:

#!/usr/bin/env python3
# coding: utf-8

import csv

description_index = None
before_description = []
after_description = []

# Open input file and read each line
with open('data.csv') as infile:
lines = infile.read().splitlines()

# Iterate through read lines and look for ' description'
# since we need to split output format based on this line number
for lno, line in enumerate(lines):
if ' description' in line:
description_index = lno

# if there is no ' description' we do not want to change output format
# and write all data to a row each.
# In order to do so, we set description_index to the length of the lines list.
if not description_index:
description_index = len(lines)

# Knowing the line number of ' description' gives us the
# possibility to divide lines into two parts
before_description = lines[:description_index]
after_description = lines[description_index:]

# Open output file, init a csvwriter and write data.
# Since a row is defined by a list passed to the writer
# we iterate through the lines before the ' description'
# and cast each element as a list before passing it to the csvwriter.
# Since the part after ' description' should be in one row we write all
# all elements to a single list.
# Since we do not want to write empty lines, we check if each element exists.
with open('output.csv', 'w', newline='') as outfile:
outputwriter = csv.writer(outfile)
for element in before_description:
if element:
outputwriter.writerow([element])
outputwriter.writerow([element for element in after_description if element])
<小时/>

输出(逗号分隔的 csv 文件):

Info
interface Aux0/0/1
inferface Vlanif30
description Public Link to CX600-3-B, ip address 10.132.10.132 255.255.255.252

关于python - 写入下一列 CSV 模块 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33687867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com