Save the same content to .csv file as print command

冷暖自知 提交于 2019-12-11 11:14:42

问题


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

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

They are saving me only one row on csv file

This is my code:

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 ?

result contains:

['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']"]

@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])) 

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


回答1:


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(';'))

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.



来源:https://stackoverflow.com/questions/36834400/save-the-same-content-to-csv-file-as-print-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!