简体   繁体   中英

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:

[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.

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 ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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