简体   繁体   English

带有特殊字符的字符串

[英]Strings with special characters

I have to check for the presence of a substring in a string in python. 我必须检查python字符串中是否存在子字符串。 The problem comes from the fact that the substring contains a special character. 问题出在以下事实:子字符串包含特殊字符。

I am reading a feature from a csv file. 我正在从csv文件读取功能。 The feature is a distance with numbers and its units: 该特征是具有数字及其单位的距离:

12.4 miles
34 Kilómetros
800 metros

I have to read the feature, check the units and convert to metres. 我必须阅读功能,检查单位并转换为米。

for line in filename:
    if 'miles' in line:  #checking for miles is straight forward
       #do whatever I have to do
    if 'Kilómetros' in line:  #the problem is here
       #do whatever I have to do

Komodo will not let me save my .py file because of the special character in Kilómetros . 由于Kilómetros中的特殊字符,科莫多不允许我保存.py文件。 Any help? 有什么帮助吗? Even if Komodo let me save the file, would this work? 即使Komodo让我保存文件,这也行吗?

Komodo attempts to detect and set which encoding your file is using when the file is first opened. 首次打开文件时,Komodo会尝试检测并设置文件使用的编码。 It might have missed the mark. 它可能错过了标记。 You can see which encoding Komodo chose in the status bar at the top of the text editing area. 您可以在文本编辑区域顶部的状态栏中看到Komodo选择的编码。 Click the drop down to change it. 点击下拉菜单进行更改。

Komodo中的编码设置。注意屏幕截图来自IDE,但“编码”下拉菜单位于同一位置。

For future Komodo questions you should use the Komodo Forums . 对于将来的科莫多问题,您应该使用科莫多论坛

Do the exact opposite. 完全相反。 Check if all the characters are in a list you want. 检查所有字符是否都在所需的列表中。 eg 例如

text1 = 'abcabcabcabcabcabcabcabcabcabc'
for char in text1:
    if char not in ['a', 'b', 'c']:
       print('oops',text1)
text2 = 'abcabcabcabcaΞΞΞbcabcabcabcabcabc'
for char in text2:
    if char not in ['a', 'b', 'c']:
       print('oops',text2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM