Luke
Luke

Reputation: 25

Hidden character causing carriage return in txt file

In the Example.txt file here, there are two words. "closed containers". When I try to copy and paste this into a word document there is a carriage return between the "a" and the "i" so it comes out as the following:

enter image description here

instead of:

closed containers.

This also occurs when copying into excel, it makes two cells stacked on top of each other like this

excel example

This problem is persistent throughout a larger .txt file, but this is just an example. In notepad, if I highlight the "ai" in containers and copy and paste it into find and replace and replace it with a typed "ai", then that fixes the problem, but this isn't feasible with the larger text file.

I'm trying to use pandas to get a dataframe with this data, but with these fake carriage returns, they are creating new rows.

Upvotes: 0

Views: 549

Answers (1)

Jonathan Leon
Jonathan Leon

Reputation: 5648

When opening the file it looks like this:

f = open('Example.txt')
f.read()

'closed conta\niners.\n'

if you do this:

f = open('Example.txt')
     text = f.read().replace('\n', '')
     print(text)

closed containers.

The problem arises, potentially, is if you really want those carriage returns at the end, then you'll have to process the file and replace differently

Upvotes: 0

Related Questions