I would like to import a text file, with 14 columns with the symbol ";" as a column separator. So, if we have 14 columns, it means we'll find 13 ";" separators in every line/row.
The problem is, as you can see below, there is a row, with a ";" character inside of one of the columns.
It makes this line to have 14 ";" separators (translated to 15 columns).
If we open the file in Excel, we can see this wrong "extra" column there:
Trying to read this file with "read_csv" pandas function, it raises an error telling that you have 15 columns (instead of 14).
So, before using the read_csv function I would like to open the file, find the rows with 14 ";" separators in the line, modify/replace the 13th ocurrence of the ";" symbol with a "." and writing back in another text file.
As an string is immutable in Python, theoretically, I don't know how to continue with my Python script. Doubting if the solution is appending in a list or pasting text. By the way, this code will be part of a final script, because this problem will happen more times in the future.
# Creating an empty list to store all the lines from the file will be readed.
listoflines = []
# Opening the file.txt in only "reading" model
with open(r"D:\file.txt", mode='r') as reader:
for line in reader.readlines():
listoflines.append(line)
# Creating an empty list to store all the lines including the modified lines.
finaldocument = []
# Detecting the row(s) where the count of the ";" symbol is equal to 14.
for row in listoflines:
if row.count(';') == 14:
# Creating a counter.
n=0
# Iterating over the letters of the row
for letter in row:
if letter == ';':
n+=1
# We need to detect the 13rd ";" symbol of the row.
if n==13:
print(letter)
letter = letter.replace(';','.')
print(letter)
## HOW TO APPEND/REPLACE THE MODIFIED LETTER TO THE ROW???? ##
# Storing, again, all the lines (including modified lines) to a final object.
finaldocument.append(row)
print(finaldocument)