从python中的文本文件中获取特定的行和数据

时间:2018-10-05 18:16:07

标签: python text-files

我是python的新手,正在尝试学习。我有一个包含以下内容的文本文件:

df <- data.frame(
  lon = c(119.4, 119.4, 119.4, 119.5, 119.5),
  lat = c(-5.192, -5.192, -5.187, -5.187, -5.191)
)
library(tidyverse)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3
polygon <- df %>%
  st_as_sf(coords = c("lon", "lat"), crs = 4326) %>%
  summarise(geometry = st_combine(geometry)) %>%
  st_cast("POLYGON")
polygon
#> Simple feature collection with 1 feature and 0 fields
#> geometry type:  POLYGON
#> dimension:      XY
#> bbox:           xmin: 119.4 ymin: -5.192 xmax: 119.5 ymax: -5.187
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
#>                         geometry
#> 1 POLYGON ((119.4 -5.192, 119...

plot(polygon)

我希望能够输入名称并获得第一,第三,第四以及第三和第四列的乘积。

我知道怎么问这个名字,但是我不能再进一步了。我想像是文本拆分和for循环之类的东西。

这是我到目前为止所做的

1 Lebron 30 5
1 Curry 29 8
1 Durant 20 4
2 Lebron 35 3
2 Curry 39 6
2 Durant 15 8
3 Lebron 25 6
3 Curry 30 5
3 Durant 21 5

1 个答案:

答案 0 :(得分:1)

您可以尝试执行以下操作(假设文件中每行只有3个空格):

file_name = input("Enter the file name: ")
filter_name = input("Enter a name for filtering: ")
filter_name = filter_name.lower()

with open(file_name) as f:
    print('Showing names that contain "{}"'.format(filter_name))
    print('{:4s} {:10s} {:10s} {:10s}'.format(
        'game', 'points', 'assists', 'p x a'))

    for line in f:
        line = line.strip()
        if len(line) > 0:
            game, name, col3, col4 = line.split()
            name = name.lower()

            if filter_name in name:
                col3 = int(col3)   # this may raise ValueError if it is not a valid int
                col4 = int(col4)   # this may raise ValueError if it is not a valid int
                product = col3 * col4

                print('{:4s} {:10d} {:10d} {:10d}'.format(
                    game, col3, col4, product))