如何从Python中类似的字符串中获取值?

2024-09-28 23:46:07 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有以下字符串,来自包含类似字符串的文件:

Andorra la Vella|ad|Andorra la Vella|20430|42.51|1.51|
Canillo|ad|Canillo|3292|42.57|1.6|
Encamp|ad|Encamp|11224|42.54|1.57|
La Massana|ad|La Massana|7211|42.55|1.51|
...

如何使用正则表达式打印第一个数字(或每个字符串的第四个字段)? 如果第4个数字超过10000,我如何打印特定行(例如“Andorr la Vella”“ad”“Andorr la Vella”20430)的前4个字段?你知道吗


Tags: 文件字符串数字laadandorravellacanillo
3条回答

你不需要正则表达式。你知道吗

s = """
Andorra la Vella|ad|Andorra la Vella|20430|42.51|1.51|
Canillo|ad|Canillo|3292|42.57|1.6|
Encamp|ad|Encamp|11224|42.54|1.57|
La Massana|ad|La Massana|7211|42.55|1.51|
"""

for line in s.splitlines():  # pretend we are reading from a file
    if not line:
        continue # skip empty lines

    groups = line.split('|')  # splits each line into its segments
    if int(groups[3]) > 10000:  # checks if the 4th value is above 10000
        print groups[:4]  # prints the first 4 values
    else:
        print groups[3]  # prints the 4th value

>>> 
['Andorra la Vella', 'ad', 'Andorra la Vella', '20430']
3292
['Encamp', 'ad', 'Encamp', '11224']
7211

我认为在这种情况下使用csv模块会更容易:

import csv
with open(filename, 'rb') as f:
    for row in csv.reader(f, delimiter='|'):
        num = float(row[3])
        if num > 10000:
            print(row[:4])

使用正则表达式

import re
results = [re.match('(.*?\|)(.*?\|)(.*?\|)(.*?\|)(.*?\|)(.*?\|)', line).groups() for line in open('file.txt')]
# filter just the rows with fourth column > 10000
results = [result for result in results if int(result[3]) > 10000]

使用拆分

results = [line.split('|')[0:-1] for line in open('file.txt')]
# filter just the rows with fourth column > 10000
results = [result for result in results if int(result[3]) > 10000]

相关问题 更多 >