Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
960 views
in Technique[技术] by (71.8m points)

csv - How to prevent escaped newlines in split string (python)

I'm writing a script that will convert a CSV to JSON. One of my spreadsheet cells contains '<,>' delimited strings. I'd like to convert this to a list. I have the following line in my script handling that cell:

quest["preMessages"] = [x.strip() for x in row["preMessages"].split('<,>')]

where row["preMessages"] represents the text in the cell.

It mostly works except for one case where my cell input is the following:

I'm receiving orders from headquarters!<,>
<THE_COMPANY> assigns tasks to the <SHIP_NAME> from time to time. 

 Best to complete them as soon as possible before they take corrective measures.

My output is:

"preMessages": [
"I'm receiving orders from headquarters!", 
"<THE_COMPANY> assigns tasks to the <SHIP_NAME> from time to time.\n\n Best to complete them as soon as possible before they take corrective measures."
],

The problem is the newlines in the middle. I want them to stay as non-escaped newlines. When I print row["preMessages"] right before this line, it looks normal like it did in the spreadsheet.

Even when I remove the call to strip() the problem persists so it must be the split. But I provided my own delimiter so I'm not sure why it's messing with the new lines.

How can I keep my newlines intact?

question from:https://stackoverflow.com/questions/65950768/how-to-prevent-escaped-newlines-in-split-string-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...