简体   繁体   中英

python - remove line breaks when importing csv

I am trying to import some data from a csv. However, for some reason I have some line breaks at the beginning of some of my data. I am using this code to import:

import csv

def opencsv():
    with open('static/urls.csv', encoding="utf-8-sig", newline='') as csvfile:
        lines = csv.reader(csvfile, skipinitialspace=True, delimiter=',', quotechar='|')
        for row in lines:
            csvrows = ''.join(row)

It all works fine until there is a line break (I am talking about line break like this '\\n' and not a blank space) in the data that I try to import.

Whenever there is a line break my csvrows will looke like this

cvsrows = '"'

ignoring all the other characters that were initially in the cell of the csv. Any idea how I can get rid of the linebreak when importing?

Does this solve your problem?

        import pandas as pd
        df=pd.read_csv(filename).dropna().reset_index().drop(['index'], axis=1)

Also please share a sample of your file as it helps us in recreating the issue and solving it.

尝试将csvrows = ''.join(row)替换为csvrows = ''.join(row) . csvrows = ''.join(row.strip())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM