How to find and replace strings within a *.txt file with Unicode encoding using Python?How do I copy a file in Python?How do I parse a string to a float or int in Python?How do I trim whitespace from a Python string?Twitter image encoding challengeConvert a Unicode string to a string in Python (containing extra symbols)Find all files in a directory with extension .txt in PythonHow do you append to a file in Python?How do I lowercase a string in Python?How to find if directory exists in PythonHow to avoid encoding parameter when opening file in Python3
How is it possible that Gollum speaks Westron?
Did thousands of women die every year due to illegal abortions before Roe v. Wade?
Is it legal in the UK for politicians to lie to the public for political gain?
Whats the next step after commercial fusion reactors?
Does an ice chest packed full of frozen food need ice? 18 day Grand Canyon trip
After the loss of Challenger, why weren’t Galileo and Ulysses launched by Centaurs on expendable boosters?
Is there any word or phrase for negative bearing?
Credit card offering 0.5 miles for every cent rounded up. Too good to be true?
Do any instruments not produce overtones?
What can plausibly explain many of my very long and low-tech bridges?
Company did not petition for visa in a timely manner. Is asking me to work from overseas, but wants me to take a paycut
Why doesn't my simple mesh wall "difference boolean cut" the door away?
Traffic law UK, pedestrians
Their answer is discrete, mine is continuous. They baited me into the wrong answer. I have a P Exam question
Pay as you go Or Oyster card
Can a 2nd-level sorcerer use sorcery points to create a 2nd-level spell slot?
PhD student with mental health issues and bad performance
Movie where a boy is transported into the future by an alien spaceship
What makes linear regression with polynomial features curvy?
Does the "6 seconds per round" rule apply to speaking/roleplaying during combat situations?
Through what methods and mechanisms can a multi-material FDM printer operate?
How do I write "Show, Don't Tell" as an Asperger?
What happens if you do emergency landing on a US base in middle of the ocean?
Adding two lambda-functions in C++
How to find and replace strings within a *.txt file with Unicode encoding using Python?
How do I copy a file in Python?How do I parse a string to a float or int in Python?How do I trim whitespace from a Python string?Twitter image encoding challengeConvert a Unicode string to a string in Python (containing extra symbols)Find all files in a directory with extension .txt in PythonHow do you append to a file in Python?How do I lowercase a string in Python?How to find if directory exists in PythonHow to avoid encoding parameter when opening file in Python3
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to replace "FullName" with a user input string within all *.txt files in the same folder. The *.txt files seem to be Unicode (shown Unicode when I click File -> Save as).
Every time I run the code it'd replace the "FullName" with some incorrect symbols (e.g. lots of ഀഀ, and ഀ䘀甀氀氀一愀洀攀ഀ䨀漀戀倀漀猀椀琀椀漀渀ഀ䐀㨀ꀀ㌀ꀀ㠀㠀).
When I made a random *.txt file and saved it as ANSI or saved any of the original file as ANSI, the replacement worked just fine.
Can somebody please help me understand what went wrong here?
fullName = input('Full Name: ')
import glob
fullName = input('Full Name: ')
for f in glob.glob('*.txt'):
with open(f, 'r') as inputfile:
newText = inputfile.read().replace('FullName', fullName)
with open(f, 'w') as outputfile:
outputfile.write(newText)
Unicode Encoding:
Aftermaths of replacement:
python unicode encoding
add a comment |
I am trying to replace "FullName" with a user input string within all *.txt files in the same folder. The *.txt files seem to be Unicode (shown Unicode when I click File -> Save as).
Every time I run the code it'd replace the "FullName" with some incorrect symbols (e.g. lots of ഀഀ, and ഀ䘀甀氀氀一愀洀攀ഀ䨀漀戀倀漀猀椀琀椀漀渀ഀ䐀㨀ꀀ㌀ꀀ㠀㠀).
When I made a random *.txt file and saved it as ANSI or saved any of the original file as ANSI, the replacement worked just fine.
Can somebody please help me understand what went wrong here?
fullName = input('Full Name: ')
import glob
fullName = input('Full Name: ')
for f in glob.glob('*.txt'):
with open(f, 'r') as inputfile:
newText = inputfile.read().replace('FullName', fullName)
with open(f, 'w') as outputfile:
outputfile.write(newText)
Unicode Encoding:
Aftermaths of replacement:
python unicode encoding
Welcome to Stack Overflow! Please take our tour, it only takes a minute. Then, if you have more time check our help center for better understanding of the site.
– Ender Look
Mar 24 at 14:48
add a comment |
I am trying to replace "FullName" with a user input string within all *.txt files in the same folder. The *.txt files seem to be Unicode (shown Unicode when I click File -> Save as).
Every time I run the code it'd replace the "FullName" with some incorrect symbols (e.g. lots of ഀഀ, and ഀ䘀甀氀氀一愀洀攀ഀ䨀漀戀倀漀猀椀琀椀漀渀ഀ䐀㨀ꀀ㌀ꀀ㠀㠀).
When I made a random *.txt file and saved it as ANSI or saved any of the original file as ANSI, the replacement worked just fine.
Can somebody please help me understand what went wrong here?
fullName = input('Full Name: ')
import glob
fullName = input('Full Name: ')
for f in glob.glob('*.txt'):
with open(f, 'r') as inputfile:
newText = inputfile.read().replace('FullName', fullName)
with open(f, 'w') as outputfile:
outputfile.write(newText)
Unicode Encoding:
Aftermaths of replacement:
python unicode encoding
I am trying to replace "FullName" with a user input string within all *.txt files in the same folder. The *.txt files seem to be Unicode (shown Unicode when I click File -> Save as).
Every time I run the code it'd replace the "FullName" with some incorrect symbols (e.g. lots of ഀഀ, and ഀ䘀甀氀氀一愀洀攀ഀ䨀漀戀倀漀猀椀琀椀漀渀ഀ䐀㨀ꀀ㌀ꀀ㠀㠀).
When I made a random *.txt file and saved it as ANSI or saved any of the original file as ANSI, the replacement worked just fine.
Can somebody please help me understand what went wrong here?
fullName = input('Full Name: ')
import glob
fullName = input('Full Name: ')
for f in glob.glob('*.txt'):
with open(f, 'r') as inputfile:
newText = inputfile.read().replace('FullName', fullName)
with open(f, 'w') as outputfile:
outputfile.write(newText)
Unicode Encoding:
Aftermaths of replacement:
python unicode encoding
python unicode encoding
edited Mar 24 at 16:04
Pikachu the Parenthesis Wizard
2,15981629
2,15981629
asked Mar 24 at 14:43
TinTin
82
82
Welcome to Stack Overflow! Please take our tour, it only takes a minute. Then, if you have more time check our help center for better understanding of the site.
– Ender Look
Mar 24 at 14:48
add a comment |
Welcome to Stack Overflow! Please take our tour, it only takes a minute. Then, if you have more time check our help center for better understanding of the site.
– Ender Look
Mar 24 at 14:48
Welcome to Stack Overflow! Please take our tour, it only takes a minute. Then, if you have more time check our help center for better understanding of the site.
– Ender Look
Mar 24 at 14:48
Welcome to Stack Overflow! Please take our tour, it only takes a minute. Then, if you have more time check our help center for better understanding of the site.
– Ender Look
Mar 24 at 14:48
add a comment |
1 Answer
1
active
oldest
votes
Windows saves Unicode in UTF-16 by default, so try opening the file with encoding='utf-16'
:
for f in glob.glob('*.txt'):
with open(f, 'r', encoding='utf-16') as inputfile:
newText = inputfile.read().replace('FullName', fullName)
with open(f, 'w', encoding='utf-16') as outputfile:
outputfile.write(newText)
This will perform badly if the file is big. A better approach in terms of memory would be to read and write a line at a time.
– tripleee
Mar 24 at 14:53
I was just pointing out the key cause of the OP's issue, but yes, the OP should avoid unnecessarily reading into memory the entire file for better memory efficiency.
– blhsing
Mar 24 at 14:55
1
This worked perfectly! Thank you so much. Also thanks for pointing out the better approach. In my case the files are simply email signature so the file size is always small. Really appreciate the help!
– Tin
Mar 25 at 13:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324958%2fhow-to-find-and-replace-strings-within-a-txt-file-with-unicode-encoding-using%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Windows saves Unicode in UTF-16 by default, so try opening the file with encoding='utf-16'
:
for f in glob.glob('*.txt'):
with open(f, 'r', encoding='utf-16') as inputfile:
newText = inputfile.read().replace('FullName', fullName)
with open(f, 'w', encoding='utf-16') as outputfile:
outputfile.write(newText)
This will perform badly if the file is big. A better approach in terms of memory would be to read and write a line at a time.
– tripleee
Mar 24 at 14:53
I was just pointing out the key cause of the OP's issue, but yes, the OP should avoid unnecessarily reading into memory the entire file for better memory efficiency.
– blhsing
Mar 24 at 14:55
1
This worked perfectly! Thank you so much. Also thanks for pointing out the better approach. In my case the files are simply email signature so the file size is always small. Really appreciate the help!
– Tin
Mar 25 at 13:08
add a comment |
Windows saves Unicode in UTF-16 by default, so try opening the file with encoding='utf-16'
:
for f in glob.glob('*.txt'):
with open(f, 'r', encoding='utf-16') as inputfile:
newText = inputfile.read().replace('FullName', fullName)
with open(f, 'w', encoding='utf-16') as outputfile:
outputfile.write(newText)
This will perform badly if the file is big. A better approach in terms of memory would be to read and write a line at a time.
– tripleee
Mar 24 at 14:53
I was just pointing out the key cause of the OP's issue, but yes, the OP should avoid unnecessarily reading into memory the entire file for better memory efficiency.
– blhsing
Mar 24 at 14:55
1
This worked perfectly! Thank you so much. Also thanks for pointing out the better approach. In my case the files are simply email signature so the file size is always small. Really appreciate the help!
– Tin
Mar 25 at 13:08
add a comment |
Windows saves Unicode in UTF-16 by default, so try opening the file with encoding='utf-16'
:
for f in glob.glob('*.txt'):
with open(f, 'r', encoding='utf-16') as inputfile:
newText = inputfile.read().replace('FullName', fullName)
with open(f, 'w', encoding='utf-16') as outputfile:
outputfile.write(newText)
Windows saves Unicode in UTF-16 by default, so try opening the file with encoding='utf-16'
:
for f in glob.glob('*.txt'):
with open(f, 'r', encoding='utf-16') as inputfile:
newText = inputfile.read().replace('FullName', fullName)
with open(f, 'w', encoding='utf-16') as outputfile:
outputfile.write(newText)
answered Mar 24 at 14:48
blhsingblhsing
46.9k51747
46.9k51747
This will perform badly if the file is big. A better approach in terms of memory would be to read and write a line at a time.
– tripleee
Mar 24 at 14:53
I was just pointing out the key cause of the OP's issue, but yes, the OP should avoid unnecessarily reading into memory the entire file for better memory efficiency.
– blhsing
Mar 24 at 14:55
1
This worked perfectly! Thank you so much. Also thanks for pointing out the better approach. In my case the files are simply email signature so the file size is always small. Really appreciate the help!
– Tin
Mar 25 at 13:08
add a comment |
This will perform badly if the file is big. A better approach in terms of memory would be to read and write a line at a time.
– tripleee
Mar 24 at 14:53
I was just pointing out the key cause of the OP's issue, but yes, the OP should avoid unnecessarily reading into memory the entire file for better memory efficiency.
– blhsing
Mar 24 at 14:55
1
This worked perfectly! Thank you so much. Also thanks for pointing out the better approach. In my case the files are simply email signature so the file size is always small. Really appreciate the help!
– Tin
Mar 25 at 13:08
This will perform badly if the file is big. A better approach in terms of memory would be to read and write a line at a time.
– tripleee
Mar 24 at 14:53
This will perform badly if the file is big. A better approach in terms of memory would be to read and write a line at a time.
– tripleee
Mar 24 at 14:53
I was just pointing out the key cause of the OP's issue, but yes, the OP should avoid unnecessarily reading into memory the entire file for better memory efficiency.
– blhsing
Mar 24 at 14:55
I was just pointing out the key cause of the OP's issue, but yes, the OP should avoid unnecessarily reading into memory the entire file for better memory efficiency.
– blhsing
Mar 24 at 14:55
1
1
This worked perfectly! Thank you so much. Also thanks for pointing out the better approach. In my case the files are simply email signature so the file size is always small. Really appreciate the help!
– Tin
Mar 25 at 13:08
This worked perfectly! Thank you so much. Also thanks for pointing out the better approach. In my case the files are simply email signature so the file size is always small. Really appreciate the help!
– Tin
Mar 25 at 13:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324958%2fhow-to-find-and-replace-strings-within-a-txt-file-with-unicode-encoding-using%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to Stack Overflow! Please take our tour, it only takes a minute. Then, if you have more time check our help center for better understanding of the site.
– Ender Look
Mar 24 at 14:48