TmSmth
TmSmth

Reputation: 452

Imported text from CSV wrapped in Excel cells

Python 3.6 here. I am writing tweets into a csv with those three lines of code (I removed unnecessary code) :

self.csvTwitter = open("twitterDB.csv", 'a', newline='', encoding='utf-8')
wr = csv.writer(self.csvTwitter, quoting=csv.QUOTE_ALL)
wr.writerow(listeInfosTweet)

listeInfoTweets contains a list of string like below :

["966305843376476162","1519220240812","Wed Feb 21 13:37:20 +0000 2018","Bloomberg","New York and the World","4617407","Spotify's Daniel Ek is special, but not Mark Zuckerberg special","en"]

Then I need to access and process a bit this csv file to write in excel. I use panda to load the data and xlwings to write it :

laData = pd.read_csv('twitterDB - 22042018.csv', encoding = "UTF-8")

The problem is that in excel, some cells are wrapped automatically like below, how i could avoid that and remove leading and trailing space and break line ? Thanks !

enter image description here

Upvotes: 3

Views: 920

Answers (2)

TmSmth
TmSmth

Reputation: 452

I found a workaround, after writing the text in the cell I write :

ws.range((i, 6)).api.WrapText = False

Upvotes: 0

max
max

Reputation: 4521

Your tweets contain new line characters like "\n"

Try this to remove them:

# replaces all occurrences of '\n' with empty string ''
my_str= my_str.replace('\n','')

You will need to iterate over all the strings and apply this function.

If it is in a Pandas column, try the apply function

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html

Upvotes: 1

Related Questions