Python Forum
It does truncatte where i would ... - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: It does truncatte where i would ... (/thread-23406.html)



It does truncatte where i would ... - AyUniz - Dec-28-2019

Hello !

I tried to do something that truncatte a list of strings but i doesn't truncate where i would to.

I show you what i would like:

(https://prnt.sc/qgww3f)
In the red rectangle is the thing i'd like to delete, but here what i have:

(https://prnt.sc/qgwwui)
How can I delete those ":" please ?

Here is the code:

import re

i = 0
fileString = input("Nom du fichier qui doit etre clean: ")
fileString = fileString.translate({ord('"'): None})
file = open(fileString, "r+")
logs = []
for line in file:
	if i < 4:
		i += 1
	else:
		logs.append(line[line.find(":"):])
file.seek(0)
file.truncate()
file.writelines(logs)
file.close()

Thank you for answering :) ♥



RE: It does truncatte where i would ... - perfringo - Dec-28-2019

I don’t understand what and why this code does. But it seems to me that the way rows are structured enables simple approach to get needed part of string:

>>> s = “0xc624 (4): 1842”
>>> s.rsplit(maxsplit=1)[1]
1842



RE: It does truncatte where i would ... - AyUniz - Dec-28-2019

(Dec-28-2019, 09:45 PM)perfringo Wrote: I don’t understand what and why this code does. But it seems to me that the way rows are structured enables simple approach to get needed part of string:

>>> s = “0xc624 (4): 1842”
>>> s.rsplit(maxsplit=1)[1]
1842

I just would like the start of the strings to be removed until "(x):"
And sometimes, there is others things like "0x4562"


RE: It does truncatte where i would ... - perfringo - Dec-29-2019

From sample data I observe that needed part of the string is after first space from right. If so to the whole dataset then rsplit is obvious choice.