[英]How to match up x and y values then separate them by a comma, and have the output go to a new csv file?
I have two lists, an X and a Y. I want to have those lists create a file with the values written as x,y.我有两个列表,一个 X 和一个 Y。我想让这些列表创建一个文件,其中的值写为 x,y。
Right now I can get them to write to a file, but its just individual values in each excel cell and I need it to be x,y.现在我可以让它们写入文件,但它只是每个 excel 单元格中的单个值,我需要它是 x,y。
Here's what I've got so far:这是我到目前为止所得到的:
import random as rd
X = []
Y = []
npts = 1000
#print (rd.random()*1000.00)
L = range(npts)
for n in L:
x = (rd.random()*1000.00)
X.append(x)
y = (rd.random()*1000.00)
Y.append(y)
#print X
#print Y
import numpy as np
#print min(X)
#print max(X)
#print min(Y)
#print max(Y)
#print np.mean(X)
#print np.mean(Y)
Xsub = []
Ysub = []
mxSubX = (np.mean(X) + 100)
mnSubX = (np.mean(X) - 100)
mxSubY = (np.mean(Y) + 100)
mnSubY = (np.mean(Y) - 100)
#print mxSubX
#print mnSubX
#print mxSubY
#print mnSubY
for n in X:
if (n > mnSubX)and (n < mxSubX):
Xsub.append(n)
#print Xsub
for n in Y:
if (n > mnSubY)and (n < mxSubY):
Ysub.append(n)
#print Ysub
f = open('lab4.csv','w')
for n in Xsub:
f.write(Xsub,Xsub)
f.close()
Edit:编辑:
for n in Xsub:
f.write(Xsub,Xsub)
his is my attempt at putting values from those two lists as x,y (comma as a delimiter) in the same cell in excel.他是我尝试将这两个列表中的值作为 x,y (逗号作为分隔符)放在 excel 的同一个单元格中。 It was not a good attempt.
这不是一个好的尝试。 My desired output is something like:
我想要的输出是这样的:
>Xsub value, Ysub value
>Xsub value, Ysub value
>Xsub value, Ysub value
>Xsub value, Ysub value
Edit 2: This is what ened up working:编辑2:这就是工作的结果:
f2 = open('subXY.txt','w')
I2 = range(1000)
for i in I:
#print (str(Xsub[i]) + ',' + str(Ysub[i]))
f2.write(str(Xsub[i]) + ',' + str(Ysub[i]))
After you have created Xsub
& Ysub
, you can write the pair (Xsub[i], Ysub[i])
in the file in the following way:你已经创建后
Xsub
& Ysub
,你可以写一对(Xsub[i], Ysub[i])
以下列方式在文件中:
with open('lab4.csv','w') as f:
for x, y in zip(Xsub, Ysub):
f.write("%s,%s\n" % (x, y))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.