0

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.

5
Contributors
14
Replies
15
Views
5 Years
Discussion Span
Last Post by toritza
Featured Replies
  • 1

    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. Read More

  • 1

    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()) Read More

1

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.

0

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.

1

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

Edited by woooee: n/a

0

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

0

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 ?

Edited by toritza: n/a

0

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 .

0

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']
"""
0

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.

0

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

Edited by snippsat: n/a

0

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?

0

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()
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.