简体   繁体   中英

Strings with special characters

I have to check for the presence of a substring in a string in python. The problem comes from the fact that the substring contains a special character.

I am reading a feature from a csv file. 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 . Any help? Even if Komodo let me save the file, would this work?

Komodo attempts to detect and set which encoding your file is using when the file is first opened. 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. 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)

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