0

I need to do a simple code as follow below but I unable to overcome how insert raw data in list.

lsttemp = []
with open (f'{dirworklnx}/logid0101039426.txt', 'r', encoding='UTF-8') as var01:
    lstgerada = var01.readline().removeprefix('{').removesuffix('}').strip().split(', ')
    for m in lstgerada:
        dadosdic02 = m.strip().rstrip().lstrip().replace('}','').replace('\'','').replace(' ','')
        chave01 = fR"{(dadosdic02[0:-2])}"
        
        print(chave01)
        # until here , that´s ok.
        # I have the string which I need to insert in my list "lsttemp"

        lsttemp.append(str(chave01))
        # THIS THE POINT! it´s always add scape characters as "\\".
        # I Should like that my data was raw data as is in original
        # string from variable "chave01"

    print(lsttemp)

Output of this code is:

The first print:

.\\aguser
user.martins
scan
migrate

The second print:

['.\\\\aguser', 'user.martins', 'scan', 'migrate']

It adds two more "\" in string ".\aguser".

How can I insert raw data in list regardless special characters?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    The string only contains two backslash characters, also after you added it to the list. You insert "raw" data into a list just like you insert any other data into a list. – mkrieger1 Jul 15 '23 at 12:43
  • 1
    See https://stackoverflow.com/questions/24085680/why-do-backslashes-appear-twice – mkrieger1 Jul 15 '23 at 12:45
  • Have you tried `print(lsttemp[0])`? You'll then see the string is just inserted into the list as-is. – Weijun Zhou Jul 15 '23 at 13:11

1 Answers1

0

thanks guys, after some test I found out the issue. when I do get request to API and I save data in this file '{dirworklnx}/logid0101039426.txt' it already supply in with special characters .\\aguser insteded .\aguser.

The issue is from file '{dirworklnx}/logid0101039426.txt' which is beyond me.

So we can close this case.

Bellow is my scrip which figure out the issue ( remove \ ), I´m using translate({ord('\'): None}) method

import os

dirworklnx = os.path.join('C:\Users\userxxx\PycharmProjects\estudo1\xxxxx') os.chdir(dirworklnx)

dic0101039426 = {} dic0100032002 = {}

def Lerlogid0101039426():

with open(f'{dirworklnx}/logid0101039426.txt', 'r', encoding='UTF-8') as f1:
    lstgerada01 = f1.readline().removeprefix('{').removesuffix('}').strip()
    var01 = lstgerada01.split(', ')
    lstgerada = []

    for m in var01:
        lstgerada.append(m.translate({ord('\\'): None}))  > THIS IS THE POINT. HERE I REMOVE "\\"

    if (lstgerada[0] != '') and (lstgerada[0] != '}'):

        for m in lstgerada:
            dadosdic02 = (m.replace('}','')).strip().rstrip().lstrip().replace('\'','').replace(' ','').split(':')
            dictemp = {dadosdic02[0]: int(dadosdic02[1])}
            dic0101039426.update(dictemp)

Lerlogid0101039426()

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 19 '23 at 21:05