【发布时间】:2019-08-07 03:04:41
【问题描述】:
如何创建一个新列,并在 csv 文件上使用 Python 为每个相应的机场记录编写首字母缩略词?
我有一个机场的 csv 文件,我希望机场的名称采用首字母缩写形式,以便我可以更紧凑地在地图上显示它们,机场符号显示它是什么。
一个例子是这个示例列表:
['Bradley Sky Ranch'、'Fire Island Airport'、'Palmer Municipal Airport']
进入这个:['B.S.R', 'F.I.A.', 'P.M.A.']
接下来,'.' 怎么写?每个首字母缩略词之间的句点标点?
我认为应该是 + "." + 或 ".".join 的名称?
最后,如果有办法去掉“机场”这个词,让每个首字母缩略词都不以“A”结尾,那将是一个好处?
例如,.strip 'Airport' 之类的...但这不是主要目标。
下面的编号列表显示了我拥有的代码示例,但我没有一致的解决方案。所以请只选择有意义的,如果没有,我想学习更有效的语法!
[原始机场数据来自 ESRI Living Atlas。] 我有一个名为“NameAbbrev”的新字段/列,我想在其中写入首字母缩写词,但我在 ArcPro 中执行此操作,它本质上包含一个黑盒界面用于计算新字段。
旁注:如果这与地图相关,为什么我要发布到 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 文件上执行 readlines: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()
有没有更简单的方法来结合这些想法并获得我想要的首字母缩写词列?或者有其他方法吗?
【问题讨论】:
-
每个机场至少有两个官方的三四个字符代码用于识别。例如,对于帕尔默市政机场,它们是 PAQ 和 PAAQ。对于有一定航空知识的人来说,如果您使用其他代码,至少会感到困惑。
标签: python list csv abbreviation acronym