python - get the last column in every file using open
问题描述
how can i get the last column of an open file. I mean, it isn't the problem to do this with a panda dataframen. I using for this problem the open methode to get only a few needed information out of different txt files. The mean problem is, that the result_metar is in different columns and isn't fixed on column 21.
for xx in range(len(fns_land)):
with open(fns_land[xx]) as infile:
for line in infile:
result_station.append(line.split(',')[0])
result_date.append(line.split(',')[1])
result_metar.append(line.split(',')[21])
is there a way to using iloc for example and change the fixed value 21?
The answer i found only show pandas solutions.
Thanks a lot.
Best
解决方案
Here you can return the last column of each open file regardless of the size of your file:
for xx in range(len(fns_land)):
with open(path) as infile:
lines = infile.readlines()
for line in lines:
result_station.append(line.split(',')[0])
result_date.append(line.split(',')[1])
result_metar.append(line.split(',')[-1])
推荐阅读
- r - 使用 rstanarm 包中的 stan_aov 缺少预测变量计算贝叶斯方差分析
- android - 如何在Android中显示较小的第二个布局保持可见的主要布局?
- azure - azure b2c 和应用服务中客户端密码和重定向 URI 和应用的用途是什么
- babeljs - Eslint 允许基于 glob 模式的多个解析器
- sql-server - 通过sql diff工具比较两个表时,参数数据类型xml对len函数的参数1无效
- primefaces - 对 PrimeFaces SelectOneMenu 上的 noSelectionOption 应用不同的样式
- python - 如何使用视图模型保存获取请求的值
- javascript - 如何在DOM(Javascript)中从递归函数转换为循环(for while等)
- sql-server - 遍历多个模型中的记录
- c# - 如何从 Azure KeyVault 中获取 SecureString - C#