0

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) 
7
  • If you're using with open, don't close your files - that's handled automatically. Commented Mar 17, 2015 at 20:53
  • That's what I thought, I just tried adding it to see if it made a difference. Commented Mar 17, 2015 at 20:55
  • 1
    you need to use .extend(list) if you want to add a list to a list :-) Commented Mar 17, 2015 at 21:03
  • 2
    ...I'm almost mad at you for how easy that was. I had already spent an hour trying a million things. If you use this as an answer I'll mark it as best, thank you my friend. Commented Mar 17, 2015 at 21:05
  • 1
    hehe, don't worry, we're all spending hours debugging/chasing classy-stupid-me mistakes somedays :-) Commented Mar 17, 2015 at 21:06

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.