python - Printing a dictionary without newlines? -


i'm trying formatting right on print-out , it's trickier should be. objective have code read in dictionary, turn list, sort it, print out text file looking like

"string" "float"

"string" "float"

"string" "float"

when instead prints

string

float

string

float

looking @ raw data dictionary looks like:

{'blahblah\n': 0.3033367037411527, 'barfbarf\n': 0.9703779366700716,  

i suspect \n newline command has this. can't seem mitigate it. code follows:

#open text file , read h = open('file1.txt', 'r') my_dict = eval(h.read())  #print out dictionary print "now tidying data......" z = my_dict  #turn dictionary list , print j = open('file2.txt', 'w') z = z.items() z.sort(key=lambda t:t[1]) z.reverse() user in z:     print >> j, user[0], user[1] j.close() 

this code works in every other part of program. reason it's having issues here.

the \n newline character. written file shows line break. should remove before printing out:

print >> j, user[0].strip(), user[1].strip() 

or better, while turning list:

z = [item.strip() item in z.items()] 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -