我目前工作的一个项目,我在这里的任务声明,现在是我需要指定设备是否是2G还是不是基于波段在Bands栏中给出。例如,过滤特定的词来获得一个特定的关键字作出,如果在python
Device ID |Bands|2G(New added column)
123 |GSM 1800, GSM 700 |
124 | GSM 1800, GSM 700, GSM 1, LTE TDD |
125 | TD-SCDMA,1 SIM |
126 |GSM850 (GSM800),WCDMA FDD Band I,WCDMA FDD Band VIII,2 SIM |
所以,如果列“带”仅包含单词“GSM”,那么它是2G的,否则,N.
我使用re模块都试过,但我停留在一些点。
import re
import csv
...
two_G_only = []
...
with open('filepath.txt', "rU") as f:
reader = csv.DictReader(f, delimiter = "|")
for row in reader:
...
...
if 'GSM' in row['Bands']:
gsm_only = " ".join(re.findall("[a-zA-Z]+", row['Bands']))
#Im stuck at here because I don't know how to test whether there is only GSM or else
else:
two_G_only.append('N')
...
...
我需要什么样的结果
Device ID | Bands | 2G
123 | GSM 1800, GSM 700 | Y
124 |GSM 1800, GSM 700, GSM 1, LTE TDD | N
125 |TD-SCDMA,1 SIM | N
126 |GSM850 (GSM800),WCDMA FDD Band I,WCDMA FDD Band VIII,2 SIM|N
谢谢你在前进,做评论,如果我的问题是不理解。我已经在网站上搜索了一些解决方案,但我确信所问的问题不是同一个问题/概念。
其实这是我的错,因为我想让它更容易阅读,所以我改变从管道分隔到选项卡或空间中的原始数据分隔。问题编辑。 – yunaranyancat