简体   繁体   中英

Python: Text File Reading Special Characters Issues

appreciate any help I can get. I have searched page after page and have not found a solution that works for my code. Sorry to ask a some what of a redundant question.

I am using Python 3.6.0 and for the life of me cannot get the darn thing to read my special characters. I have a text file with "ā" and am trying to have my module read how many of ā are in the line and the location of them. I have the text file saved as utf-8 encoding and have added utf-8 encoding just about everywhere I can think and it still will not read that the character exists. I am not getting a trace back error or any error at all, that's probably why I'm stumped.

import sys
import re
# coding=utf-8 
with open("text1.txt","r", encoding='utf-8') as rf, open('text2.txt','w', encoding='utf-8') as wf:
        y = 'ā'
        for line in rf:
                VarTest = line.count(y)
                        if VarTest == 1:
                        VarLocation = [pos for pos, char in enumerate(line) if char == y]

The counter will not count that the character was on the line and I'm pretty sure my code for "VarLocation" is incorrect, but VarTest won't even read/count the darn thing.

Any help would be appreciated thank you!

HaHa yes, thank you. I also realized I had 2.7.13 python open and as soon as I closed it. The module started working both ways. Thank you again!!

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