我正在尝试创建一个python脚本,它将从文本文件中读取数据,然后检查是否有(两个字母),这会告诉我是否是国家代码。我尝试过使用split和其他方法,但没有成功?以下是我到目前为止的代码-->
# Python program to
# demonstrate reading files
# using for loop
import re
file2 = open('contry.txt', 'w')
file3 = open('noncountry.txt', 'w')
# Opening file
file1 = open('myfile.txt', 'r')
count = 0
noncountrycount = 0
countrycounter = 0
# Using for loop
print("Using for loop")
for line in file1:
count += 1
pattern = re.compile(r'^\.\w{2}\s')
if pattern.match(line):
print(line)
countrycounter += 1
else:
print("fail", line)
noncountrycount += 1
print(noncountrycount)
print(countrycounter)
file1.close()
file2.close()
file3.close()
txt文件中有这个
.aaa generic American Automobile Association, Inc.
.aarp generic AARP
.abarth generic Fiat Chrysler Automobiles N.V.
.abb generic ABB Ltd
.abbott generic Abbott Laboratories, Inc.
.abbvie generic AbbVie Inc.
.abc generic Disney Enterprises, Inc.
.able generic Able Inc.
.abogado generic Minds + Machines Group Limited
.abudhabi generic Abu Dhabi Systems and Information Centre
.ac country-code Internet Computer Bureau Limited
.academy generic Binky Moon, LLC
.accenture generic Accenture plc
.accountant generic dot Accountant Limited
.accountants generic Binky Moon, LLC
.aco generic ACO Severin Ahlmann GmbH & Co. KG
.active generic Not assigned
.actor generic United TLD Holdco Ltd.
.ad country-code Andorra Telecom
.adac generic Allgemeiner Deutscher Automobil-Club e.V. (ADAC)
.ads generic Charleston Road Registry Inc.
.adult generic ICM Registry AD LLC
.ae country-code Telecommunication Regulatory Authority (TRA)
.aeg generic Aktiebolaget Electrolux
.aero sponsored Societe Internationale de Telecommunications Aeronautique (SITA INC USA)
我现在得到这个错误 文件“C:/Users/tyler/Desktop/Python Class/findcountrycodes/Test.py”,第15行,在文件1的第23行:文件“C:\Users\tyler\AppData\Local\Programs\Python\Python36\lib\encodings\cp1252.py”,在解码返回codecs.charmap\u解码(输入,self.errors,解码表)[0]UnicodeDecodeError:“charmap”编解码器无法对8032位置的字节0x90进行解码:字符映射到
这是你一直在寻找的东西吗:
简言之:
file.readlines()
在本例中,返回文件中所有行的列表,几乎将文件按\n
分割然后,对于这些行中的每一行,它都会被更多的空格分开,而且由于您需要的代码是行中的第一个,因此它也是列表中的第一个,因此现在检查列表中的第一个项目是否有3个字符长是很重要的,因为由于您的格式似乎非常一致,只有3个长度才是国家代码
您正在
three spaces
上拆分,但是字符代码后面只跟一个空格,因此您的逻辑是错误的检查第三个字符是否不是空格,第四个字符是否是空格
这通常不仅仅是代码的问题,所以我们需要所有的上下文来重现、调试和解决
编码错误
最后一个提示是您粘贴的控制台输出(错误,stacktrace)
阅读stacktrace&;研究
这就是我的阅读方式&;分析错误输出(Python的stacktrace):
从这个输出中,我们可以提取重要的上下文信息,以便进行研究和分析;解决问题:
Test.py
指向读取文件的错误语句:file1 = open('myfile.txt', 'r')
UnicodeDecodeError
,这是在读取文件时经常发生的Python Exception您现在可以:
UnicodeDecodeError
李>尝试不同的编码
一个答案建议使用当今常见的UTF-8:
open(filename, encoding="utf8")
检测文件编码
一种有条理的解决办法是:
encoding
打开Python代码的文件另见:
过滤国家代码
因此,您只需要带有
country-code
s的行预期的筛选
然后期望输入文件的3行被过滤:
使用正则表达式的解决方案
正如您已经做的那样,测试文件的每一行。 测试行是否以这4个字符
.xx
开头(其中xx
可以是任何ASCII字母)正则表达式解释
此正则表达式测试有效的两个字母的国家/地区代码:
^
从字符串的开始(line
)\.
(第一个)字母应该是点\w{2}
(后跟)任意两个单词字符(⚠️ 也匹配_0
)\s
(后跟)一个空格(空白、制表符等)Python代码
这是在您的代码中完成的,如下所示(假设
line
是从读取行填充的):这是一个可运行的demo on IDEone
进一步阅读
相关问题 更多 >
编程相关推荐