将相同的内容保存为.csv文件作为打印命令 [英] Save the same content to .csv file as print command

查看:181
本文介绍了将相同的内容保存为.csv文件作为打印命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打印命令显示我这样的东西(我有更多的3行,但这只是一部分的例子):

Print command showing me something like this (I have more as 3 rows, but this is only part of example):

O;4;Line[63];CPUA.DB1610.4122,X1;RW
V;5;Enable_maintenance_mode;;
V;5;Maintenance_start_motor;CPUA.DB1610.4124,X0;RW
O;4;Line[64];CPUA.DB1610.4124,X1;RW

我试图保存相同的内容在csv文件,但在csv我只有一行和这种格式。问题是,如何保存到csv文件保存什么打印显示?

I'm trying save the same content on csv file, but on csv I have only one row and this format. The question is, how to save to the csv file the save what print showing ?

O,;,4,;,L,i,n,e,[,6,4,],;,C,P,U,A,.,D,B,1,6,1,0,.,4,1,2,4,|,|,X,1,;,R,W

他们在csv上只保存一行文件

They are saving me only one row on csv file

这是我的代码:

for i in range((len(result)-1)):
 print(str(result[i][0]) + ';' + str(result[i][1]) + ';' + result[i][2] + ';' + str(adress[i]))
 with open(source_file[:-4] + '_test.csv', "w") as f:
  writer = csv.writer(f)
  writer.writerow(str(result[i][0]) + ';' + str(result[i][1]) + ';' + result[i][2] + ';' + str(adress[i]))

做错了。有人可以帮我解决这个问题吗?

I don't know what I'm doing wrong. Can someone help me solve this problem ?

结果包含:

['V', '5', 'Enable_maintenance_mode', "S7:[CPUA]DB1610', 'X4124.0", 'B', 'RW', '0', "0']"]
['V', '5', 'Maintenance_start_motor', "S7:[CPUA]DB1610', 'X4124.1", 'B', 'RW', '0', "0']"]
['O', '4', 'Line[64]', '', '', "']"]
['V', '5', 'Enable_maintenance_mode', "S7:[CPUA]DB1610', 'X4126.0", 'B', 'RW', '0', "0']"]


b $ b




@Martin Evans你的例子看起来真的很好。但我只需要从结果行的一部分,并保存为csv。
我可以somethink这样?


@Martin Evans Your example look really good. But I need only part of row from result and save as csv. Can I make somethink like this ?

csv_output = csv.writer(f_output, lineterminator='\n')
 for line in result:
 #csv_output.writerow(line)
 csv_output.writerow(str(result[line][0]) + ';' + str(result[line][1]) + ';' + result[line][2] + ';' + str(adress[line])) 

但我有错误:TypeError:list indexes必须是整数,而不是列表

But I have error: TypeError: list indices must be integers, not list

推荐答案

库设计为获取列条目的列表,并通过为您添加必需的定界符将其自动转换为整个csv行。您的打印语句通过在每个列条目之间附加; 来模拟这一点。您可以指定创建 csv.writer 时使用的分隔符,在本例中为;

The csv library is designed to take a list of column entries and convert it automatically into a whole csv row by adding the necessary delimiters for you. Your print statement is simulating this by appending the ; between each column entry. You can specify the delimiter to use when creating the csv.writer, in your example this is ;:

import csv

result = [
    ['V', '5', 'Enable_maintenance_mode', "S7:[CPUA]DB1610', 'X4124.0", 'B', 'RW', '0', "0']"],
    ['V', '5', 'Maintenance_start_motor', "S7:[CPUA]DB1610', 'X4124.1", 'B', 'RW', '0', "0']"],
    ['O', '4', 'Line[64]', '', '', "']"],
    ['V', '5', 'Enable_maintenance_mode', "S7:[CPUA]DB1610', 'X4126.0", 'B', 'RW', '0', "0']"]]

address = [
    "CPUA.DB1610.4122,X1;RW",
    "",
    "CPUA.DB1610.4124,X0;RW",
    "CPUA.DB1610.4124,X1;RW"]

with open('output.csv', 'w', newline='') as f_output:
    csv_output = csv.writer(f_output, delimiter=';')

    for r, a in zip(result, address):
        csv_output.writerow(r[0:3] + a.split(';'))

在Python 3.x中使用 csv.writer()时,应始终使用 w 并设置 newline =''。此外,更容易直接迭代您的结果条目,而不使用 range()和索引。由于您似乎有两个相等长度的列表,您可以使用 zip 在每次迭代时为您提供一个元素。

When using a csv.writer() in Python 3.x, you should always open the file with w and set newline=''. Also it is easier to iterate through your result entries directly without using range() and indexing. As you appear to have two equal length lists, you can use zip to give you an element from each as you iterate.

这将给你一个输出文件如下:

This would give you an output file as follows:

V;5;Enable_maintenance_mode;CPUA.DB1610.4122,X1;RW
V;5;Maintenance_start_motor;
O;4;Line[64];CPUA.DB1610.4124,X0;RW
V;5;Enable_maintenance_mode;CPUA.DB1610.4124,X1;RW

请注意,您尚未提供地址的示例,因此这是基于您的示例输出。

Note, you have not provided a sample for address so this is based on your example output.

这篇关于将相同的内容保存为.csv文件作为打印命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆