简体   繁体   中英

encode csv lines in csv file to utf8

I'm looking for a way to encode URL saved in lines of my csv file to utf8, but couldnt till now find the right library to do it, the idea will be a library that read from csv lines and then encode to utf8 to put it may be in an other file or new column,

Any body have an idea?

to add an example:

I have a file that contain one column which is details:

containing some text, that I need after to pass in a url but encoded in utf.

like one line is:

Créez, testez et déployez des applications sur Oracle Cloud — gratuitement. Inscrivez-vous une fois et accédez à deux offres gratuites.

and the result expected is for this line is:

Cr%C3%A9ez%2C%20testez%20et%20d%C3%A9ployez%20des%20applications%20sur%20Oracle%20Cloud%20%E2%80%94%20gratuitement.%20Inscrivez-vous%20une%20fois%20et%20acc%C3%A9dez%20%C3%A0%20deux%20offres%20gratuites.

this is just an example of one line in my csv file, I need to apply this for all the line,

well I found a solution and its working but not that correct:

import pandas as pd
from urllib.parse import quote

data = pd.read_csv("file_decoded.csv",error_bad_lines=False)


def title_parse(details):
    details = quote(details)
    return details


data['details']= data.details.apply(title_parse)
data.to_csv('file_encoded.csv')

the issue with this function is the text is encoded but it does take it as ascci code base, I dont know how to explain it

import pandas as pd
data = pd.read_csv("filename.csv")
data.to_csv("filename_new.csv", encoding="utf-8")

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