Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python problem reading file
#1
Hello,

I am reading a file that has over 200 thousand lines with users information.

When it finds lines with "|" it makes a split to separate the fields I identified as "name" that contains name and surname, followed by IP, MAC, etc.

If the field contains the string enter inside a word it performs a line break by error, when it should save the line with name, ip, mac, etc:

JUAN ARMENTEROS IP MAC
PEDRO REMENTER IP MAC

but with the line break python code generates two lines in the csv:

JUAN ARMENTER
OS IP MAC
PEDRO REMENTER
IP MAC

Recording in the database the disordered fields of the lines involved, duplicating its. I guess python interprets ENTER as the reserved word and then executes it.

How can I indicate that ignore this word and not executes it?

Thank you.



df = pd.DataFrame(columns=('extension','empleado','mac_address','validado','neqt','ipv4_address','dominio','nodo'))

num_cs = 0
with open('/shared/scripts/01_extraccion.txt', 'r', encoding="utf8", errors='ignore') as file:
    iloc_number = 0
    for linea in file:
        iloc_number = iloc_number + 1

        if "Welcome to cpuanode" in linea:
            nodo = linea[19:21]

        if "------------------------------------------------IP couplers defined in domain " in linea:
            dominio = linea[78:81].replace(" ","")
        campos = linea.split("|")

        if len(campos) > 10:
            if "QMCDU" not in campos[1]:

                extension = campos[1].strip()
                empleado = campos[2].strip()
                mac_address = campos[3].strip()
                validado = campos[4].strip()
                neqt = campos[5].strip()
                ipv4_address = campos[7].strip()
                dominio = dominio.strip()
                nodo = nodo
                print(extension,empleado,mac_address,validado,neqt,ipv4_address,dominio,nodo)
                df = df.append([{'extension':extension, 
                                'empleado':empleado,
                                'mac_address':mac_address,
                                'validado':validado,
                                'neqt':neqt,
                                'ipv4_address':ipv4_address,
                                'dominio':dominio,
                                'nodo':nodo}], 
                                ignore_index=True)          
    print(df)
file.close()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad problems with reading csv file. MassiJames 3 632 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 916 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,102 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 1,095 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 989 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 897 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 989 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Failing reading a file and cannot exit it... tester_V 8 1,818 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Problem with importing Python file in Visual Studio Code DXav 7 5,087 Jun-15-2022, 12:54 PM
Last Post: snippsat
  Reading .csv file doug2019 4 1,703 Apr-29-2022, 09:55 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020