2

I would like to replace the first row of my csv file. The csv file is generated by a script that opens other csv files and reads some columns. It looks like

TimeStamp;Value;Value; ...
2014/08/04 21:00:53.575;0.168889;1.146; ...
2014/08/04 21:01:23.590;0.168889;1.138; ...
2014/08/04 21:01:53.595;0.17;1.154; ...
2014/08/04 21:02:23.585;0.168889;1.205; ...

I would like to replace the first row (Timestamp;Value;Value) with the names is saved in a list.

The output I want should be

TimeStampfromlist;Firstnamefromlist;Secondnamefromlist; ...
2014/08/04 21:00:53.575;0.168889;1.146; ...
2014/08/04 21:01:23.590;0.168889;1.138; ...
2014/08/04 21:01:53.595;0.17;1.154; ...
2014/08/04 21:02:23.585;0.168889;1.205; ...

How can I do that?


Trying to be more specific this is the code I use to generate my csv file

import csv
import glob
import os, sys

path = "C:/Users/ButoeruG/Desktop/pythonscripts/prova"
dirs = glob.glob('*.csv')
namelist = dirs

print dirs[0]

file1 = dirs[1]
print file1
for file in namelist:
    namelist = file.partition("TrendLogExtended_")[2].partition("-Ext).csv")[0]
    print namelist


primofile = csv.reader(open(file1, 'rb'), delimiter=";", quotechar='|')
output_rows = []

for row in primofile:
    output_rows.append([row[2], row[15]])

for file in dirs:
    data = csv.reader(open(file, 'rb'), delimiter=";", quotechar='|')
    column = []
    for idx,row in enumerate(data):
        output_rows[idx].append(row[15])

with open("provaoutput.csv", 'wb') as f:
    writer = csv.writer(f, delimiter=';')
    for row in output_rows:
        writer.writerow(row)

Now the point is that when I write the row[15] I just copy from a file a column that looks like:

Value;
1,956;
1;054;
1,456;

I would like to replace value with part of the file name that I saved in namelist.

0

2 Answers 2

4

You can use the the csv module but this logic will do what you need.

l = ["TimeStampfromlist","Firstnamefromlist","Secondnamefromlist"]

with open("in.csv", 'r') as data_file:
    lines = data_file.readlines()
    lines[0]= ":".join(l)+"\n" # replace first line, the "header" with list contents
    with open("in.csv", 'w') as out_data:
        for line in lines: # write updated lines
            out_data.write(line)
Sign up to request clarification or add additional context in comments.

5 Comments

This breaks if the new first line is shorter than the old one because one will end up with some garbage at the end of the file with the length of the difference of the two first lines, old and new. Apart from binary or fixed width records it is almost always better and safer to write a new file when the file size changes as a result.
@BlackJack, it replaces the first line with the new line, readlines reads all the lines individually so replacing element 0 with the list contents will work so I have no idea what you are talking about
If the new first line is shorter than the old one then the file's content gets shorter, or at least it should but it doesn't on disk. It stays the same size and has a part of the last line(s) as garbage attached at the end, because that data was there from the beginning and doesn't magically disappear. Say the file contains 'aaa\nbbb\nccc\n' and 'aaa\n' is replaces by 'x\n', then the file contains 'x\nbbb\nccc\nc\n' afterwards.
@GabrielButoeru, no worries, you're welcome, there is no problem with the logic of this approach despite the comments.
I had some index problem, but it was my fault. Thanks again ;)
1

The general approach is open file for reading, skip the old header line, open target file for writing, write the new header line, then copy over the rest of the file. At the end rename new file to old file.

import os

new_headers = ['TimeStampfromlist', 'Firstnamefromlist', 'Secondnamefromlist']
filename = 'data.csv'
with open(filename, 'r') as lines:
    next(lines)  # Skip first line.
    tmp_filename = filename + '.tmp'
    with open(tmp_filename, 'w') as out_file:
        out_file.write(':'.join(new_headers) + '\n')
        out_file.writelines(lines)
    os.rename(tmp_filename, filename)

3 Comments

When I try to add new_headers it prints just "T,i,m,e,S,t,a,m,p,f,r,o,m,l,i,s,t ... why?
@GabrielButoeru Most probably because your new_headers isn't a list of new headers but a string. Try ';'.join(['spam', 'eggs']) vs. ';'.join('spam,eggs') to see the difference.
I solved using for file in namelist: namelist[namelist.index(file)] = file.partition("TrendLogExtended_")[2].partition("-Ext).csv")[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.