简体   繁体   English

Python:如何将值从另一个csv文件写入一个csv文件

[英]Python: How to write values to a csv file from another csv file

For index.csv file, its fourth column has ten numbers ranging from 1 - 5 . 对于index.csv文件,它的第四列有十个数,从1 - 5 Each number can be regarded as an index, and each index corresponds with an array of numbers in filename.csv . 每个数字都可以视为一个索引,并且每个索引与filename.csv的数字数组相对应。

The row number of filename.csv represents the index, and each row has three numbers. filename.csv的行号表示索引,每行有三个数字。 My question is about using a nesting loop to transfer the numbers in filename.csv to index.csv . 我的问题是关于使用嵌套循环将filename.csv的数字传输到index.csv

from numpy import genfromtxt
import numpy as np
import csv
import collections

data1 = genfromtxt('filename.csv', delimiter=',')
data2 = genfromtxt('index.csv', delimiter=',')

out = np.zeros((len(data2),len(data1)))

for row in data2:

    for ch_row in range(len(data1)):

        if (row[3] == ch_row + 1):

            out = row.tolist() + data1[ch_row].tolist()

            print(out)

            writer = csv.writer(open('dn.csv','w'), delimiter=',',quoting=csv.QUOTE_ALL)

            writer.writerow(out)

For example, the fourth column of index.csv contains 1,2,5,3,4,1,4,5,2,3 and filename.csv contains: 例如, index.csv的第四列包含1,2,5,3,4,1,4,5,2,3filename.csv包含:

# filename.csv

20 30 50 
70 60 45 
35 26 77 
93 37 68 
13 08 55 

What I need is to write the indexed row from filename.csv to index.csv and store these number in 5th, 6th and 7th column: 我需要写的是从filename.csvindex.csv的索引行,并将这些数字存储在第5、6和7列中:

# index.csv

#   4 5  6  7 
... 1 20 30 50 
... 2 70 60 45 
... 5 13 08 55 
... 3 35 26 77 
... 4 93 37 68 
... 1 20 30 50 
... 4 93 37 68 
... 5 13 08 55 
... 2 70 60 45 
... 3 35 26 77

If I do "print(out)", it comes out a correct answer. 如果我执行“ print(out)”,它会给出正确的答案。 However, when I input "out" in the shell, there are only one row appears like [1.0, 1.0, 1.0, 1.0, 20.0, 30.0, 50.0] 但是,当我在外壳程序中输入“ out”时,只有一行显示为[1.0,1.0,1.0,1.0,20.0,30.0,50.0]

What I need is to store all the values in the "out" variables and write them to the dn.csv file. 我需要将所有值存储在“输出”变量中,并将它们写入dn.csv文件。

with open('dn.csv','w') as f:
    writer =  csv.writer(f, delimiter=',',quoting=csv.QUOTE_ALL)
        for row in data2:
            idx = row[3]
            out = [idx] + [x for x in data1[idx-1]]
            writer.writerow(out)

This ought to do the trick for you: 这应该为您解决问题:

Code: 码:

from csv import reader, writer


data = list(reader(open("filename.csv", "r"), delimiter=" "))

out = writer(open("output.csv", "w"), delimiter=" ")

for row in reader(open("index.csv", "r"), delimiter=" "):
    out.writerow(row + data[int(row[3])])

index.csv: index.csv:

0 0 0 1
0 0 0 2
0 0 0 3

filename.csv: filename.csv:

20 30 50
70 60 45
35 26 77
93 37 68
13 08 55

This produces the output: 产生输出:

0 0 0 1 70 60 45
0 0 0 2 35 26 77
0 0 0 3 93 37 68

Note : There's no need to use numpy here. 注意 :这里不需要使用numpy。 The stadard library csv module will do most of the work for you. 标准库csv模块将为您完成大部分工作。

I also had to modify your sample datasets a bit as what you showed had indexes out of bounds of the sample data in filename.csv . 我还必须对样本数据集进行一些修改,因为显示的filename.csvfilename.csv中超出了样本数据的范围。

Please also note that Python ( like most languages ) uses 0th indexes. 另请注意,Python( 与大多数语言一样 )使用第0个索引。 So you may have to fiddle with the above code to exactly fit your needs. 因此,您可能不得不摆弄上面的代码以完全满足您的需求。

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

相关问题 如何从csv文件中选择值并使用python将其写入另一个csv文件 - how to select values from csv file and write it to another csv file using python 如何在没有熊猫的情况下在python中的另一个csv文件中写入一个csv文件 - how to write a csv file in another csv file in python without pandas 如何使用另一个 csv 文件中的数据写入现有的 CSV 文件? - How to write in an existing CSV file with data from another csv file? 如何从python中的生成器写入.csv文件 - How to write in .csv file from a generator in python 用python从csv字典写入csv文件 - with python write to csv file from csv dictionary Python:如何从另一个csv文件更新一个csv文件 - Python: how to update a csv file from another csv file Python Pandas:如何从另一个csv文件更新一个csv文件 - Python Pandas: how to update a csv file from another csv file 用python处理csv文件(读取一个csv,根据值需要做出决定并将其写入另一个csv文件) - Dealing csv files with python(reading one csv,based on values need to take decision and write it to another csv file) 如何从CSV文件加载数据,对其进行操作并将其写入Python中的另一个CSV中 - How to load data from a CSV file, manipulate it, and write it to another CSV in Python 如何在Python中编写.csv文件? - How to write .csv file in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM