使用Python在CSV文件中的特定列上编写 [英] Use Python to write on specific columns in csv file

查看:98
本文介绍了使用Python在CSV文件中的特定列上编写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件中有数据,我需要将其写到特定列中的CSV文件中.文件中的数据是这样的:

I have data in a file and I need to write it to CSV file in specific column. The data in file is like this:

002100
002077
002147

我的代码是这样的:

import csv

f = open ("file.txt","r")
with open("watout.csv", "w") as output:
    for line in f :
       c.writerows(line)

它总是写在第一列上.我该如何解决? 谢谢.

It is always writes on the first column. How could I resolve this? Thanks.

推荐答案

这就是我解决问题的方式

This is how I solved the problem

f1 = open ("inFile","r") # open input file for reading

with open('out.csv', 'wb') as f: # output csv file
    writer = csv.writer(f)
    with open('in.csv','r') as csvfile: # input csv file
        reader = csv.reader(csvfile, delimiter=',')
        for row in reader:  
            row[7] = f1.readline() # edit the 8th column 
            writer.writerow(row)
f1.close()   

这篇关于使用Python在CSV文件中的特定列上编写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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