简体   繁体   English

如何匹配 x 和 y 值,然后用逗号分隔它们,并将输出转到新的 csv 文件?

[英]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:你已经创建后XsubYsub ,你可以写一对(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.

相关问题 如何用逗号与csv分隔 - How to separate by comma with csv 需要时间 (X) 值是静态的以匹配 CPU 值 (Y) - Need time (X) values to be static to match up with CPU values (Y) 如何匹配一行的第一个单词并用逗号分隔后续值 - How to match the first word of a line and separate subsequent values with a comma 如何从CSV文件中提取数据列并将其定义为x和y变量,然后使用pylab在python中绘制它们? - How can I extract columns of data from a CSV file and define them as x and y variables, then plot them in python using pylab? 如何根据值将CSV文件拆分为2个单独的CSV文件 - How to split CSV file into 2x separate CSV files based on values 如何从CSV文件中分离逗号分隔的数据? - How to separate comma separated data from csv file? 如何在两个单独列表中的 X 和 Y 值范围内计算函数 f(X,Y) 的值 - How to compute values of function, f(X,Y), over a range of values of X and Y in two separate lists 读取 CSV 文件,该文件使用双引号分隔列中的值,并使用逗号分隔 Python 中的列 - Read CSV file that uses doubles quotes to separate values within columns and comma's to separate columns in Python 我有 25.csv 文件(每个文件都是一个抄写员)都在相同的结构(X、Y 和 STATUE)中。 我想将它们全部合并到一个 large.txt 文件中 - I have 25 .csv files (each file is a scribe) all in same structure (X, Y and STATUE). I want to combine all of them into one large .txt file 如何提取与特定模式匹配的字符串的一部分,但对于一行中的所有情况,并使用 Pandas 用逗号分隔它们 - How to extract a part of string match with specific pattern but for all cases in a row and separate them by comma using pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM