简体   繁体   English

用list替换Python中的第一行

[英]Replace first row in Python with list

I would like to replace the first row of my csv file. 我想替换我的csv文件的第一行。 The csv file is generated by a script that opens other csv files and reads some columns. csv文件由打开其他csv文件并读取某些列的脚本生成。 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. 我想替换第一行(Timestamp; Value; Value),名称保存在列表中。

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 试图更具体这是我用来生成我的csv文件的代码

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: 现在重点是,当我写行[15]时,我只是从文件中复制一个如下所示的列:

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

I would like to replace value with part of the file name that I saved in namelist. 我想用我在名单中保存的部分文件名替换值。

You can use the the csv module but this logic will do what you need. 您可以使用csv模块,但此逻辑将执行您所需的操作。

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)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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