gpt4 book ai didi

python - 为Python列表中的每个机场制作首字母缩略词?

转载 作者:行者123 更新时间:2023-12-01 01:07:36 24 4
gpt4 key购买 nike

如何在 csv 文件上使用 Python 创建新列并为每个机场记录编写首字母缩略词?

我有一个机场 csv 文件,我希望机场名称采用缩写形式,以便我可以更紧凑地在 map 上显示它们,并用机场符号显示它是什么。

以下示例列表就是一个示例:

['布拉德利天空牧场'、'火岛机场'、'帕尔默市机场']

进入此:['B.S.R'、'F.I.A.'、'P.M.A.']

接下来,你会如何放置“.”每个缩写字母之间的句号标点符号?

我认为应该是+“。” + 或带有 ".".join 的内容?

最后,如果有一种方法可以去掉“Airport”这个词,这样每个首字母缩略词都不会以“A”结尾,那么会有一个好处吗?

例如,.strip“机场”之类的内容...但这不是主要目标。

下面的编号列表显示了我拥有的代码示例,但我没有一致的解决方案。因此,请仅采取有意义的内容,如果没有意义,我想学习更有效的语法!

[原始机场数据来自 ESRI Living Atlas。]我有一个名为“NameAbbrev”的新字段/列,我想将首字母缩略词写入其中,但我在 ArcPro 中执行此操作,它本质上包含一个黑盒界面用于计算新字段。

旁注:如果这与 map 相关,为什么我要发布到 SO 而不是 GeoNet?请注意,我的目标是使用 python,而不是询问 ArcPy。我认为基本原理是基于 python 来操作 csv 文件(而 ArcPy 将在要素类上操作,并且您必须使用 ESRI 指定的函数)。 SO 吸引了更广泛的 Python 专家受众。

1) 到目前为止,我已经了解了如何将字符串转换为首字母缩略词,这对于单个字符串(而不是列表)效果很好: Creating acronyms in Python

acronym = "".join(word[0] for word in test.upper().split())

2) 并尝试拆分列表中的项目,或者如何根据示例(不是我的)在 csv 文件上执行读取行:Attribute Error: 'list' object has no attribute 'split'

def getQuakeData():
filename = input("Please enter the quake file: ")
# Use with to make sure the file gets closed
with open(filename, "r") as readfile:
# no need for readlines; the file is already an iterable of lines
# also, using generator expressions means no extra copies
types = (line.split(",") for line in readfile)
# iterate tuples, instead of two separate iterables, so no need for zip
xys = ((type[1], type[2]) for type in types)
for x, y in xys:
print(x,y)

getQuakeData()

3)此外,我已经能够使用 pandas 将机场名称列打印到列表中:

import pandas
colnames = ['OBJECTID', 'POLYGON_ID', 'POLYGON_NM', 'NM_LANGCD', 'FEAT_TYPE', 'DETAIL_CTY', 'FEAT_COD', 'NAME_FIX', 'ORIG_FID', 'NameAbbrev']
data = pandas.read_csv(r'C:\Users\...\AZ_Airports_table.csv', names=colnames)

names = data.NAME_FIX.tolist()
print(names)

#Here is a sample of the list of airport names/print result.
#If you want a sample to demo guidance you could use these names:
#['NAME_FIX', 'Bradley Sky Ranch', 'Fire Island Airport', 'Palmer Municipal Airport', 'Kodiak Airport', 'Nome Airport', 'Kenai Municipal Airport', 'Iliamna Airport', 'Sitka Airport', 'Wrangell Airport', 'Sand Point Airport', 'Unalaska Airport', 'Adak Airport', 'Homer Airport', 'Cold Bay Airport']

4) 我过去也能够使用搜索光标和 writerow,但我不知道如何准确地应用这些方法。 (不相关的示例):

with open(outCsv, 'wb') as ouputCsv:  
writer = csv.writer(outputCsv)
writer.writerow(fields) # writes header containing list of fields
rows = arcpy.da.SearchCursor(fc, field_names=fields)
for row in rows:
writer.writerow(row) # writes fc contents to output csv
del rows

5)所以,我有一些碎片,但我不知道如何将它们全部组合在一起,或者它们是否能够组合在一起。这是我的弗兰肯斯坦怪物解决方案,但它是错误的,因为它试图查看每一列!

def getAcronym():
filename = r'C:\Users\...\AZ_Airports_table.csv'
# Use with to make sure the file gets closed
with open(filename, "r") as readfile:
# no need for readlines; the file is already an iterable of lines
# also, using generator expressions means no extra copies
airport = (line.split(",") for line in readfile)
# iterate tuples, instead of two separate iterables, so no need for zip
abbreviation = "".join(word[0] for word in airport.upper().split())
# could also try filter(str.isupper, line)
print(abbreviation)

getAcronym()

是否有更简单的方法来结合这些想法并获得我想要的首字母缩略词列?或者有其他方法吗?

最佳答案

使用 list comprehension 可以非常简单地完成此操作。 , str.join ,和 filter :

>>> data = ['Bradley Sky Ranch', 'Fire Island Airport', 'Palmer Municipal Airport']
>>> ['.'.join(filter(str.isupper, name)) for name in data]
['B.S.R', 'F.I.A', 'P.M.A']

关于python - 为Python列表中的每个机场制作首字母缩略词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55192681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com