[英]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,3
和filename.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.csv
到index.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.csv
在filename.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.