Python 3,将特定列从一个csv文件复制到另一个csv

时间:2016-04-18 21:37:58

标签: python csv python-3.x

我只是想将一整列数据从名为"solar.csv"的文件复制到另一个名为"current.csv"的csv文件中。我要复制的这个专栏是#14。

理想情况下,在复制之后,我想将整个"current.csv"文件输出到另一个目录。以下是我到目前为止的情况:

with open('F:\somefolder\somedata\solar.csv', 'rb') as f, open('F:\somefolder\somedata\current.csv', 'wb') as f_out:
    reader = csv.reader(f)
    writer = csv.writer(f_out)
    for row in reader:
        writer.writerow((row[14]))

1 个答案:

答案 0 :(得分:0)

我没有使用过csv模块,但是阅读csv文件应该很容易。像

with open('solar.csv', 'r') as f, open('current.csv', 'w') as f_out:
  for line in f:
    cols = line.split(',', 14)
    if len(cols) >= 14:
      f_out.write(cols[13].strip() + "\n")