1

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!

1
  • Check my new answer: I had overlooked one key issue. Commented Sep 12, 2019 at 15:19

2 Answers 2

4

Current code issues

replace doesn't work in-place. Instead it returns a string with the replacement done. Hence, for fix #1 you should change your statement from:

row[i].replace("\\n", " ")

to:

row[i] = row[i].replace("\\n", " ")

However, the bigger problem is the iteration over the list obtained from the .split(",") operation.

In fact, your iteration runs short of 1 element, hence never touches the last item, and thus never removes the \n. Let's do some math:

row = ['13.9', ' 5.2', ' 3.4\n']
# len(row)  == 3
# len(row) - 1 == 2
# range(len(row) - 1) == [0 1], which will do 2 iterations instead of 3

So, fix #2 would be to correct that for loop, which should look something like:

for row in open(filename):
    row = row.split(",")
    for i in range(len(row)):  # notice the absence of -1
        row[i] = row[i].replace("\n", "")
        row[i] = float(row[i])
    lines.append(row)

Better way

Since every line of a CSV file has an ending \n, you might as well strip it before you split the columns and perform the conversion str to float via map, like this:

lines = []
for row in open(filename):
    row = row.strip().split(",")  # first remove the "\n" then split
    row = list(map(float, row))   # [13.9, 5.2, 3.4]
    lines.append(row)
Sign up to request clarification or add additional context in comments.

2 Comments

I was really hoping this would work but unfortunately it still prints with the "\n" at the end. Thank you for trying though!
@user12055762 Check my updated answer: I had overlooked one key issue in your original code.
2

Strings are immutable in Python, so you will need to always assign row[i] back to the modified version of itself:

for row in open(filename):
    row = row.split(",")
    for i in range(len(row) - 1):
        row[i] = row[i].replace("\n", "")  # CHANGE HERE
        row[i] = float(row[i])
    lines.append(row)

Note: You don't need to double escape the backslash in \n when using regular string replace.

3 Comments

Maybe better to use .strip() as we don't need whitespace as the string is being converted to a float
I just tried that and the .strip() but it still prints the last element of each row with the '\n' at the end. Thank you so much for the suggestions though.
Please try: row[i] = row[i].replace("\n", "") ... don't replace with a space, replace with empty string.