How to remove substring from string in Python 3

后端 未结 2 1271
清酒与你
清酒与你 2021-01-28 14:41

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

2条回答
  •  春和景丽
    2021-01-28 14:54

    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.

提交回复
热议问题