6 Replies - 1371 Views - Last Post: 04 September 2014 - 09:16 PM Rate Topic: -----

#1 cairo.lievore   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-August 14

how can I sort the numbers of each line of a txt file in Python?

Posted 03 September 2014 - 07:52 PM

how can I sort the numbers of each line of a txt file in Python?

each line have 15 numbers out of order, how can I sort the numbers of each line and save it again?
would it be better to save in a new file or in the same file?
Is This A Good Question/Topic? 0
  • +

Replies To: how can I sort the numbers of each line of a txt file in Python?

#2 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11265
  • View blog
  • Posts: 19,301
  • Joined: 19-March 11

Re: how can I sort the numbers of each line of a txt file in Python?

Posted 03 September 2014 - 08:25 PM

View Postcairo.lievore, on 03 September 2014 - 09:52 PM, said:

how can I sort the numbers of each line of a txt file in Python?

each line have 15 numbers out of order, how can I sort the numbers of each line and save it again?
would it be better to save in a new file or in the same file?



Well, think through what needs to happen. You need to get the data from a file into memory - that's reading a file.
You need to get them specifically in as lines, which means you need to think about how to do that.
Then you need to sort each line - this implies that you want the lines as lists, which are easily sorted. (look it up!)
Finally, you want to send the output to a file on disk. Whether you want to overwrite the original input is entirely up to you, and the requirements you're working with. Perhaps you want to save the original data so you can compare the results to the original, in which case you want to write to a new file. Or perhaps you want to produce the impression that the same file now has sorted lines. In that case, you want to overwrite the existing file. Your choice.
Was This Post Helpful? 0
  • +
  • -

#3 cairo.lievore   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-August 14

Re: how can I sort the numbers of each line of a txt file in Python?

Posted 04 September 2014 - 05:23 PM

I tried this code:
inside file.txt has one line with these numbers:
10 12 3 4 1 6 17 25 14 5 11 16 22 21 7

import itertools

file_data = open('file.txt')

for each_line in file_data:
	s = []
	s.append(sorted(each_line))
	print(s)
file_data.close()


the out was:
$ python3 run.py
[['\n', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '3', '4', '4', '5', '5', '6', '6', '7', '7']]


Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11265
  • View blog
  • Posts: 19,301
  • Joined: 19-March 11

Re: how can I sort the numbers of each line of a txt file in Python?

Posted 04 September 2014 - 05:26 PM

Okay, so what do you think happened? Or, what didn't happen that needed to?
How do you think python understood that line, when you interpreted it in list context?
Was This Post Helpful? 0
  • +
  • -

#5 cairo.lievore   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-August 14

Re: how can I sort the numbers of each line of a txt file in Python?

Posted 04 September 2014 - 05:42 PM

I know how to sort lines, if there are only a number in each line of the file, but I get stuck when the numbers are in each line, a lot of them, when I read the file and create a list for it it put the whole line as a one index and when I sort it it sort every number of it splitting the numbers like 11, 12, 20...

$ python3 run.py
['10 12 3 4 1 6 17 25 14 5 11 16 22 21 7\n']


when I add a line inside a list, it come as one index, I just need to know how to split it to 15 index to the list than sort it.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11265
  • View blog
  • Posts: 19,301
  • Joined: 19-March 11

Re: how can I sort the numbers of each line of a txt file in Python?

Posted 04 September 2014 - 05:53 PM

As it happens, there is a method called split(), which breaks a string into an array of substrings. You might want to try that.
(you might have to look up the details)
Was This Post Helpful? 0
  • +
  • -

#7 cairo.lievore   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-August 14

Re: how can I sort the numbers of each line of a txt file in Python?

Posted 04 September 2014 - 09:16 PM

Yes, that was exactly what I did.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1