Hello. I am have a problem while trying to write to a file some sorted lines . my initial file is somthing like this
name2,number2;name3,number3;name1,number1
when i run this code

ord = open("Be.csv", 'r')
order = ord.read().split(";")
order = sorted(order)
for sort in order:
print sort
ord.close()
f=open("Odd.csv","w")
f.write(sort)
f.close()

Even if it prints in shell all the lines i want and of course sorted in the new file it will only write name3,number3
What might seem to be the problem?
if someone has any ideea i would be grateful.

Recommended Answers

All 14 Replies

Run the input files one at a time, i.e. run the program using the first file and check the output, then run the program again using the second file and check the output file, etc.

Run the input files one at a time, i.e. run the program using the first file and check the output, then run the program again using the second file and check the output file, etc.

as i am totaly new in python i do not really understand what you sugest i should do. Meanwhile i discovered on the web a new way to sort my file
lines = open("Be.csv").read().split(';')
lines.sort()
out = open("Ord.csv", "w")
for line in lines:
print line
out.write(line)
out.close()

but ..it will write the result like this name1,number1;name2,number2;name3,number3 on my newfile an not as i really want
name1,number1
name2,number2
name3,number3
So regarding the first code ..i still do not really understand how to make iti work.
Again i am sorry for putting so silly question but i am new in python.

The difference between
name1,number1;name2,number2;name3,number3
and
name1,number1
name2,number2
name3,number3
is a newline. Use rstrip() to strip the newline before you write to the output file.
out.write(line.rstrip())

The difference between
name1,number1;name2,number2;name3,number3
and
name1,number1
name2,number2
name3,number3
is a newline. Use rstrip() to strip the newline before you write to the output file.
out.write(line.rstrip())

If i use this code as you have suggested the file does not change in any way. It remains name1,number1;name2,number2;name3,number3;

lines = open("Be.csv").read().split(';')
lines.sort()
out = open("Ord.csv", "w")
for line in lines:
print line
out.write(line.rstrip())
out.close()

so what might the problem be? as i use Python 2.7

please put your code in the code tags

please put your code in the code tags

lines = open("Be.csv").read().split(';')
lines.sort()
out = open("Ord.csv", "w")
for line in lines:
    print line
    out.write(line.rstrip())
out.close()

this way ?

yep you got it.

yep you got it.

But stil i get nothing new. This is what i was trying to say ,In my new file the lines are the same name, number;name1,number2;name2,number2 and not
name,number
name1, number1
name2,number2
That was my problem, not the indentation .

Here is one way.

import re

'''num.txt-->
name2,number2;name3,number3;name1,number1
'''

with open('num.txt') as f:
    l = [i.strip().split(';') for i in f][0]

print l  #Test print

def key_sort(string):
    result = re.split(r'(\d+)', string)
    for i in xrange(1, len(result), 2):
        result[i] = int(result[i])
    return result

l.sort(key=key_sort)
print l

"""Output-->
['name2,number2', 'name3,number3', 'name1,number1']
['name1,number1', 'name2,number2', 'name3,number3']
"""

Here is one way.

import re

'''num.txt-->
name2,number2;name3,number3;name1,number1
'''

with open('num.txt') as f:
    l = [i.strip().split(';') for i in f][0]

print l  #Test print

def key_sort(string):
    result = re.split(r'(\d+)', string)
    for i in xrange(1, len(result), 2):
        result[i] = int(result[i])
    return result

l.sort(key=key_sort)
print l

"""Output-->
['name2,number2', 'name3,number3', 'name1,number1']
['name1,number1', 'name2,number2', 'name3,number3']
"""

My problem was not with printing the sorted lines as my code worked fine for printing them.
I had problems with writing those sorted lines in a new file in a specific format:

name1,number1
name2,number2
name3,number3

out.write(line.rstrip()) is the problem as it does not produce the format i want in the new file.
So if somebody has any ideea of how to solve this i would more than grateful.

This way in my code will save it in format you want.

with open('new_file.txt', 'w') as f:
    f.write('\n'.join(l))

This way in my code will save it in format you want.

with open('new_file.txt', 'w') as f:
    f.write('\n'.join(l))

Thanks for the advice, It works fine and it writes the way i want.
I have a question: how could i repair my own code

lines = open("Be.csv").read().split(';')
lines.sort()
out = open("Ord.csv", "w")
for line in lines:
    print line
    out.write(line.rstrip())
out.close()

So that i could get to the same result as you code?

You are so close! Add a few test prints and things become more clear ...

'''assume test file Be.csv is:
name2,number2;name3,number3;name1,number1
'''

fin = open("Be.csv")
text_data = fin.read()
fin.close()

print(text_data)  # test

line_list = text_data.split(';')

print(line_list)  # test

line_list.sort()

print(line_list)  # test

fout = open("Ord.csv", "w")
for line in line_list:
    print(line)
    # print() auto adds a newline char
    # write() needs to be told to do this
    fout.write(line + '\n')
fout.close()

You are so close! Add a few test prints and things become more clear ...

'''assume test file Be.csv is:
name2,number2;name3,number3;name1,number1
'''

fin = open("Be.csv")
text_data = fin.read()
fin.close()

print(text_data)  # test

line_list = text_data.split(';')

print(line_list)  # test

line_list.sort()

print(line_list)  # test

fout = open("Ord.csv", "w")
for line in line_list:
    print(line)
    # print() auto adds a newline char
    # write() needs to be told to do this
    fout.write(line + '\n')
fout.close()

Indeed it worked.
Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.