I am new to Python 3. Currently, I am working on a project that requires ne to go through a csv file (without using the csv modules) and extract numbers. While I have been able to get most of the extracting part done, my problem is that the last number of each line is printed with a "\n," which means I cannot convert it into a float. How can I get rid of this for each row?
I've tried using .rsplit("\n"), .replace("\n", " "), .replace("\\n", " "), and have even done the backslash and the n in two separate replace statements, but they still stay there.
Here's what I have at the moment:
for row in open(filename):
row = row.split(",") # elements separated by commas
for i in range(len(row) - 1): # go through each element in the row
row[i].replace("\\n", " ") # supposed to get rid of the \n at the end
row[i] = float(row[i]) # str to float conversion
lines.append(row) # add that row to list of lines
Sample csv: 13.9, 5.2, 3.4
Expected results: [13.9, 5.2, 3.4]
Actual results: [13.9, 5.2,'3.4\n']
Apologies if I formatted wrongly, this is my first time posting on Stack Overflow. Any help is appreciated, thank you!