简体   繁体   English

将列表写入文本文件而不用换行python

[英]Writing list to text file not making new lines python

I'm having trouble writing to text file. 我在写入文本文件时遇到问题。 Here's my code snippet. 这是我的代码段。

ram_array= map(str, ram_value)
cpu_array= map(str, cpu_value)
iperf_ba_array= map(str, iperf_ba)
iperf_tr_array= map(str, iperf_tr)


#with open(ram, 'w') as f:
    #for s in ram_array:
        #f.write(s + '\n')

#with open(cpu,'w') as f:
    #for s in cpu_array:
        #f.write(s + '\n')

with open(iperf_b,'w') as f:
    for s in iperf_ba_array:
        f.write(s+'\n')
    f.close()   
with open(iperf_t,'w') as f:
    for s in iperf_tr_array:
        f.write(s+'\n')
    f.close()

The ram and cpu both work flawlessly, however when writing to a file for iperf_ba and iperf_tr they always come out look like this: ram和cpu都可以正常工作,但是在为iperf_ba和iperf_tr写入文件时,它们总是看起来像这样:

[45947383.0, 47097609.0, 46576113.0, 47041787.0, 47297394.0]

Instead of 代替

1

2

3       

They're both reading from global lists. 他们俩都是从全球名单上阅读。 The cpu and ram have values appended 1 by 1, but otherwise they look exactly the same pre processing. cpu和ram的值加1到1,否则它们看起来完全一样。

Here's how they're made 这是它们的制作方法

filename= "iperfLog_2015_03_12_20:45:18_123_____tag_33120L06.csv"
write_location= self.tempLocation()
location=(str(write_location) + str(filename)); 


df = pd.read_csv(location, names=list('abcdefghi'))

transfer = df.h
transfer=transfer[~transfer.isnull()]#uses pandas to remove nan
transfer=transfer.tolist()

length= int(len(transfer))
extra= length-1

del transfer[extra]

bandwidth= df.i
bandwidth=bandwidth[~bandwidth.isnull()]
bandwidth=bandwidth.tolist()


del bandwidth[extra]

iperf_tran.append(transfer)
iperf_band.append(bandwidth)

[from comment] [来自评论]

you need to use .extend(list) if you want to add a list to a list - and don't worry: we're all spending hours debugging/chasing classy-stupid-me mistakes sometimes ;) 如果您想将列表添加到列表中,则需要使用.extend(list)-不用担心:我们有时都花大量时间调试/追寻经典,愚蠢的错误;)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM