[英]How to write a variable to a csv file without it being separated by individual characters?
My code attempts to split several data tables into year long chunks, then correlate them against all other years and return the correlation values to a matrix.我的代码尝试将几个数据表拆分为一年长的块,然后将它们与所有其他年份相关联并将相关值返回到矩阵。 I am attempting to write these outputs to a csv file, and while it is working fine for the matrices themselves, when I try to write the name of the column and table, they are split by their individual characters.我正在尝试将这些输出写入 csv 文件,虽然它对矩阵本身工作正常,但当我尝试编写列和表的名称时,它们被各自的字符分割。
def split_into_yearly_chunks(egauge, column, offset):
split_into_chunks_stmnt = " SELECT " + column + \
" FROM " + egauge + \
" OFFSET " + offset + " LIMIT 525600 "
year_long_chunk = pd.read_sql_query(split_into_chunks_stmnt, engine)
return year_long_chunk
for x in prof_list:
for y in col_list:
list_of_year_long_chunks = []
for z in off_list:
year_long_chunk = split_into_yearly_chunks(x,y,z)
if len(year_long_chunk) == 525600:
list_of_year_long_chunks.append(year_long_chunk)
corr_matrix = []
for corr_year in list_of_year_long_chunks:
corr_row = []
for corr_partner in list_of_year_long_chunks:
corr_value, p_coef = spearmanr(corr_year, corr_partner)
corr_row.append(corr_value)
corr_matrix.append(corr_row)
print(x,'; ',y,'; ')
with open('correlation_data_58_profiles.csv', 'a') as f:
thewriter = csv.writer(f)
thewriter.writerow(x)
thewriter.writerow(y)
for row in corr_matrix:
print(row)
with open('correlation_data_58_profiles.csv', 'a', newline = '') as f:
thewriter = csv.writer(f)
thewriter.writerow(row)
(Really only the last 10 or so lines in my code are the problem, but I figured I'd give the whole thing for background). (实际上只有我的代码中的最后 10 行左右是问题,但我想我会把整个事情作为背景)。 My prof,col,and off_lists are all lists of strings.我的 prof、col 和 off_lists 都是字符串列表。 The way that this stores in my csv file looks like this:它存储在我的 csv 文件中的方式如下所示:
e,g,a,u,g,e,1,3,8,3,0
g,r,i,d
1.0,0.7811790818745755,0.7678768782119194,0.7217461539833535
0.7811790818745755,0.9999999999999998,0.7614854144434556,0.714875063672517
0.7678768782119194,0.7614854144434556,0.9999999999999998,0.7215907332829061
0.7217461539833535,0.7148750636725169,0.7215907332829061,0.9999999999999998
I'd like egauge13830 and grid not to be separated by commas, and other answers I've seen wouldn't work for the for loop that I have.我希望 egauge13830 和 grid 不要用逗号分隔,我看到的其他答案不适用于我拥有的 for 循环。 How can I do this?我怎样才能做到这一点?
csv.writer(...).writerow
expects a list of values, representing the values of a single row. csv.writer(...).writerow
需要一个值列表,表示单行的值。
At some places in the code you are giving it single strings:在代码中的某些地方,你给它一个字符串:
thewriter.writerow(x); # x is a string out of prof_list
thewriter.writerow(y); # y is a string out of col_list
Since it expects lists of strings, it treats each of these strings as a list of individual characters;由于它需要字符串列表,因此它将这些字符串中的每一个都视为单个字符的列表; that's why you get each character as its own "column", separated by commas.这就是为什么您将每个字符作为自己的“列”,用逗号分隔。
If you want each of these single strings to appear in its own row as a single column value, then you'll need to make each of them into a one-element-list , indicating to the CSV writer that you want a row consisting of a single value:如果您希望这些单个字符串中的每一个作为单个列值出现在其自己的行中,那么您需要将它们中的每一个都放入一个 one-element-list 中,向 CSV 编写器指示您想要一行由单个值:
thewriter.writerow([x]); # `[x]` means "a list comprised only of x"
thewriter.writerow([y]);
Also bear in mind that a CSV containing two rows of a single value each, followed by N rows of K values each would be kind of hard to further process;还要记住,CSV 包含两行,每行一个值,然后是 N 行 K 值,每行都很难进一步处理; so you should consider if that's really what you want.所以你应该考虑这是否真的是你想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.