Extracting a few bits of data from .txt file












0















After a long morning, I have given up!



I have the following text file: StationLog.txt



Containing the following:



Version      = 2.0
StationName = STN67_P70
BeginTime = 2017-10-06.03:25:00
EndTime = 2017-10-06.03:55:00
IgnoreNo = 5000
PumpedVolume = 0


I need to extract the BeginTime, EndTime and StationName, those headings are fine, as values which feeds into another bit of code.



The idea is I don't have to do it manually as there will be lots of these files in time.



Following various other guides I have to this far:



a <- read.fwf("StationLog.txt", c(37,100), stringsAsFactors=FALSE)
a <- a[grep("=", a$V1), ]
a <- cbind(
do.call( rbind, strsplit(a$V1, "=\s+") )


But hit a bit of a wall, any help would be greatly appreciated!










share|improve this question



























    0















    After a long morning, I have given up!



    I have the following text file: StationLog.txt



    Containing the following:



    Version      = 2.0
    StationName = STN67_P70
    BeginTime = 2017-10-06.03:25:00
    EndTime = 2017-10-06.03:55:00
    IgnoreNo = 5000
    PumpedVolume = 0


    I need to extract the BeginTime, EndTime and StationName, those headings are fine, as values which feeds into another bit of code.



    The idea is I don't have to do it manually as there will be lots of these files in time.



    Following various other guides I have to this far:



    a <- read.fwf("StationLog.txt", c(37,100), stringsAsFactors=FALSE)
    a <- a[grep("=", a$V1), ]
    a <- cbind(
    do.call( rbind, strsplit(a$V1, "=\s+") )


    But hit a bit of a wall, any help would be greatly appreciated!










    share|improve this question

























      0












      0








      0








      After a long morning, I have given up!



      I have the following text file: StationLog.txt



      Containing the following:



      Version      = 2.0
      StationName = STN67_P70
      BeginTime = 2017-10-06.03:25:00
      EndTime = 2017-10-06.03:55:00
      IgnoreNo = 5000
      PumpedVolume = 0


      I need to extract the BeginTime, EndTime and StationName, those headings are fine, as values which feeds into another bit of code.



      The idea is I don't have to do it manually as there will be lots of these files in time.



      Following various other guides I have to this far:



      a <- read.fwf("StationLog.txt", c(37,100), stringsAsFactors=FALSE)
      a <- a[grep("=", a$V1), ]
      a <- cbind(
      do.call( rbind, strsplit(a$V1, "=\s+") )


      But hit a bit of a wall, any help would be greatly appreciated!










      share|improve this question














      After a long morning, I have given up!



      I have the following text file: StationLog.txt



      Containing the following:



      Version      = 2.0
      StationName = STN67_P70
      BeginTime = 2017-10-06.03:25:00
      EndTime = 2017-10-06.03:55:00
      IgnoreNo = 5000
      PumpedVolume = 0


      I need to extract the BeginTime, EndTime and StationName, those headings are fine, as values which feeds into another bit of code.



      The idea is I don't have to do it manually as there will be lots of these files in time.



      Following various other guides I have to this far:



      a <- read.fwf("StationLog.txt", c(37,100), stringsAsFactors=FALSE)
      a <- a[grep("=", a$V1), ]
      a <- cbind(
      do.call( rbind, strsplit(a$V1, "=\s+") )


      But hit a bit of a wall, any help would be greatly appreciated!







      r text automation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 9:59









      JimJim

      425




      425
























          3 Answers
          3






          active

          oldest

          votes


















          2














          Based on the read.table function, you can use the arguments to do what you want.



          The following proposal will work if you have only one = per row, and if the BeginTime, EndTime and StationName have the same writing in all files:



          read.table(
          file ="StationLog.txt",
          header =FALSE, # No column names
          sep ="=", # separator character
          strip.white =TRUE, # remove multiple white character
          row.names =1, # the first column contains the rownames
          stringsAsFactors=FALSE
          )[c("BeginTime", "EndTime", "StationName"), # extract the 3 infos based on their names corresponding to the rownames
          ,drop=FALSE] # keep the data.frame format


          And the result:



                                       V2
          BeginTime 2017-10-06.03:25:00
          EndTime 2017-10-06.03:55:00
          StationName STN67_P70





          share|improve this answer
























          • Great, that's worked a treat, I can then extract the bits needed from the df. Cheers your help!

            – Jim
            Nov 14 '18 at 10:38



















          0














          If you read the whole thing as a multiline string:



          data:



          txt_string <- "Version      = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"

          stationName<- regmatches(txt_string, gregexpr("StationName\s+=\s+\K\S+" ,txt_string, perl = T))
          beginTime <- regmatches(txt_string, gregexpr("BeginTime\s+=\s+\K\S+" ,txt_string, perl = T))
          endTime <- regmatches(txt_string, gregexpr("EndTime\s+=\s+\K\S+" ,txt_string, perl = T))

          do.call(cbind, c(stationName, beginTime, endTime))

          # [,1] [,2] [,3]
          #[1,] "STN67_P70" "2017-10-06.03:25:00" "2017-10-06.03:55:00"





          share|improve this answer
























          • Cheers for replying Andre That's the path I started down, but I can't get my head around the \s+= bit, what it means, I am sure its a method of selecting whats in there, but it puzzled me, if you wouldn't mind explaining for future reference?

            – Jim
            Nov 14 '18 at 11:50













          • Hey, it's called regular expression. You can reference what the "bits" are doing on www.regex101.com for e.g.

            – Andre Elrico
            Nov 14 '18 at 11:56











          • Great stuff! Cheers for pointing me in the right direction

            – Jim
            Nov 14 '18 at 13:54



















          0














          Alternate method:



          # use the filename vs this embedded example
          station_info <- readLines(textConnection("Version = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"))


          base:



          as.list(sapply(
          strsplit(station_info, split = "[[:space:]]*=[[:space:]]*"),
          function(x) setNames(x[2], x[1])
          )) -> station_info

          str(station_info, 1)
          ## List of 6
          ## $ Version : chr "2.0"
          ## $ StationName : chr "STN67_P70"
          ## $ BeginTime : chr "2017-10-06.03:25:00"
          ## $ EndTime : chr "2017-10-06.03:55:00"
          ## $ IgnoreNo : chr "5000"
          ## $ PumpedVolume: chr "0"


          tidyverse:



          library(tidyverse)

          # use the filename vs this embedded example
          station_info <- readLines(textConnection("Version = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"))

          str_split(station_info, pattern = "[[:space:]]*=[[:space:]]*") %>%
          map(~set_names(.x[2], .x[1])) %>%
          flatten() %>%
          str(1)


          Wrapping the base version (to avoid dependencies) into a function so you can re-use it for other stations:



          read_station_metadata <- function(path) {

          path <- path.expand(path)
          stopifnot(file.exists(path))

          station_info <- readLines(path, warn = FALSE)

          as.list(sapply(
          strsplit(station_info, split = "[[:space:]]*=[[:space:]]*"),
          function(x) setNames(x[2], x[1])
          ))

          }





          share|improve this answer
























          • Cheers for your detailed reply, my aim was to put it into a function eventually so that's ideal! Thanks, although I have just opened up a new can of worms. Many thanks

            – Jim
            Nov 14 '18 at 15:30













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297441%2fextracting-a-few-bits-of-data-from-txt-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Based on the read.table function, you can use the arguments to do what you want.



          The following proposal will work if you have only one = per row, and if the BeginTime, EndTime and StationName have the same writing in all files:



          read.table(
          file ="StationLog.txt",
          header =FALSE, # No column names
          sep ="=", # separator character
          strip.white =TRUE, # remove multiple white character
          row.names =1, # the first column contains the rownames
          stringsAsFactors=FALSE
          )[c("BeginTime", "EndTime", "StationName"), # extract the 3 infos based on their names corresponding to the rownames
          ,drop=FALSE] # keep the data.frame format


          And the result:



                                       V2
          BeginTime 2017-10-06.03:25:00
          EndTime 2017-10-06.03:55:00
          StationName STN67_P70





          share|improve this answer
























          • Great, that's worked a treat, I can then extract the bits needed from the df. Cheers your help!

            – Jim
            Nov 14 '18 at 10:38
















          2














          Based on the read.table function, you can use the arguments to do what you want.



          The following proposal will work if you have only one = per row, and if the BeginTime, EndTime and StationName have the same writing in all files:



          read.table(
          file ="StationLog.txt",
          header =FALSE, # No column names
          sep ="=", # separator character
          strip.white =TRUE, # remove multiple white character
          row.names =1, # the first column contains the rownames
          stringsAsFactors=FALSE
          )[c("BeginTime", "EndTime", "StationName"), # extract the 3 infos based on their names corresponding to the rownames
          ,drop=FALSE] # keep the data.frame format


          And the result:



                                       V2
          BeginTime 2017-10-06.03:25:00
          EndTime 2017-10-06.03:55:00
          StationName STN67_P70





          share|improve this answer
























          • Great, that's worked a treat, I can then extract the bits needed from the df. Cheers your help!

            – Jim
            Nov 14 '18 at 10:38














          2












          2








          2







          Based on the read.table function, you can use the arguments to do what you want.



          The following proposal will work if you have only one = per row, and if the BeginTime, EndTime and StationName have the same writing in all files:



          read.table(
          file ="StationLog.txt",
          header =FALSE, # No column names
          sep ="=", # separator character
          strip.white =TRUE, # remove multiple white character
          row.names =1, # the first column contains the rownames
          stringsAsFactors=FALSE
          )[c("BeginTime", "EndTime", "StationName"), # extract the 3 infos based on their names corresponding to the rownames
          ,drop=FALSE] # keep the data.frame format


          And the result:



                                       V2
          BeginTime 2017-10-06.03:25:00
          EndTime 2017-10-06.03:55:00
          StationName STN67_P70





          share|improve this answer













          Based on the read.table function, you can use the arguments to do what you want.



          The following proposal will work if you have only one = per row, and if the BeginTime, EndTime and StationName have the same writing in all files:



          read.table(
          file ="StationLog.txt",
          header =FALSE, # No column names
          sep ="=", # separator character
          strip.white =TRUE, # remove multiple white character
          row.names =1, # the first column contains the rownames
          stringsAsFactors=FALSE
          )[c("BeginTime", "EndTime", "StationName"), # extract the 3 infos based on their names corresponding to the rownames
          ,drop=FALSE] # keep the data.frame format


          And the result:



                                       V2
          BeginTime 2017-10-06.03:25:00
          EndTime 2017-10-06.03:55:00
          StationName STN67_P70






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 10:34









          BastienBastien

          1463




          1463













          • Great, that's worked a treat, I can then extract the bits needed from the df. Cheers your help!

            – Jim
            Nov 14 '18 at 10:38



















          • Great, that's worked a treat, I can then extract the bits needed from the df. Cheers your help!

            – Jim
            Nov 14 '18 at 10:38

















          Great, that's worked a treat, I can then extract the bits needed from the df. Cheers your help!

          – Jim
          Nov 14 '18 at 10:38





          Great, that's worked a treat, I can then extract the bits needed from the df. Cheers your help!

          – Jim
          Nov 14 '18 at 10:38













          0














          If you read the whole thing as a multiline string:



          data:



          txt_string <- "Version      = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"

          stationName<- regmatches(txt_string, gregexpr("StationName\s+=\s+\K\S+" ,txt_string, perl = T))
          beginTime <- regmatches(txt_string, gregexpr("BeginTime\s+=\s+\K\S+" ,txt_string, perl = T))
          endTime <- regmatches(txt_string, gregexpr("EndTime\s+=\s+\K\S+" ,txt_string, perl = T))

          do.call(cbind, c(stationName, beginTime, endTime))

          # [,1] [,2] [,3]
          #[1,] "STN67_P70" "2017-10-06.03:25:00" "2017-10-06.03:55:00"





          share|improve this answer
























          • Cheers for replying Andre That's the path I started down, but I can't get my head around the \s+= bit, what it means, I am sure its a method of selecting whats in there, but it puzzled me, if you wouldn't mind explaining for future reference?

            – Jim
            Nov 14 '18 at 11:50













          • Hey, it's called regular expression. You can reference what the "bits" are doing on www.regex101.com for e.g.

            – Andre Elrico
            Nov 14 '18 at 11:56











          • Great stuff! Cheers for pointing me in the right direction

            – Jim
            Nov 14 '18 at 13:54
















          0














          If you read the whole thing as a multiline string:



          data:



          txt_string <- "Version      = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"

          stationName<- regmatches(txt_string, gregexpr("StationName\s+=\s+\K\S+" ,txt_string, perl = T))
          beginTime <- regmatches(txt_string, gregexpr("BeginTime\s+=\s+\K\S+" ,txt_string, perl = T))
          endTime <- regmatches(txt_string, gregexpr("EndTime\s+=\s+\K\S+" ,txt_string, perl = T))

          do.call(cbind, c(stationName, beginTime, endTime))

          # [,1] [,2] [,3]
          #[1,] "STN67_P70" "2017-10-06.03:25:00" "2017-10-06.03:55:00"





          share|improve this answer
























          • Cheers for replying Andre That's the path I started down, but I can't get my head around the \s+= bit, what it means, I am sure its a method of selecting whats in there, but it puzzled me, if you wouldn't mind explaining for future reference?

            – Jim
            Nov 14 '18 at 11:50













          • Hey, it's called regular expression. You can reference what the "bits" are doing on www.regex101.com for e.g.

            – Andre Elrico
            Nov 14 '18 at 11:56











          • Great stuff! Cheers for pointing me in the right direction

            – Jim
            Nov 14 '18 at 13:54














          0












          0








          0







          If you read the whole thing as a multiline string:



          data:



          txt_string <- "Version      = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"

          stationName<- regmatches(txt_string, gregexpr("StationName\s+=\s+\K\S+" ,txt_string, perl = T))
          beginTime <- regmatches(txt_string, gregexpr("BeginTime\s+=\s+\K\S+" ,txt_string, perl = T))
          endTime <- regmatches(txt_string, gregexpr("EndTime\s+=\s+\K\S+" ,txt_string, perl = T))

          do.call(cbind, c(stationName, beginTime, endTime))

          # [,1] [,2] [,3]
          #[1,] "STN67_P70" "2017-10-06.03:25:00" "2017-10-06.03:55:00"





          share|improve this answer













          If you read the whole thing as a multiline string:



          data:



          txt_string <- "Version      = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"

          stationName<- regmatches(txt_string, gregexpr("StationName\s+=\s+\K\S+" ,txt_string, perl = T))
          beginTime <- regmatches(txt_string, gregexpr("BeginTime\s+=\s+\K\S+" ,txt_string, perl = T))
          endTime <- regmatches(txt_string, gregexpr("EndTime\s+=\s+\K\S+" ,txt_string, perl = T))

          do.call(cbind, c(stationName, beginTime, endTime))

          # [,1] [,2] [,3]
          #[1,] "STN67_P70" "2017-10-06.03:25:00" "2017-10-06.03:55:00"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 11:17









          Andre ElricoAndre Elrico

          5,67711029




          5,67711029













          • Cheers for replying Andre That's the path I started down, but I can't get my head around the \s+= bit, what it means, I am sure its a method of selecting whats in there, but it puzzled me, if you wouldn't mind explaining for future reference?

            – Jim
            Nov 14 '18 at 11:50













          • Hey, it's called regular expression. You can reference what the "bits" are doing on www.regex101.com for e.g.

            – Andre Elrico
            Nov 14 '18 at 11:56











          • Great stuff! Cheers for pointing me in the right direction

            – Jim
            Nov 14 '18 at 13:54



















          • Cheers for replying Andre That's the path I started down, but I can't get my head around the \s+= bit, what it means, I am sure its a method of selecting whats in there, but it puzzled me, if you wouldn't mind explaining for future reference?

            – Jim
            Nov 14 '18 at 11:50













          • Hey, it's called regular expression. You can reference what the "bits" are doing on www.regex101.com for e.g.

            – Andre Elrico
            Nov 14 '18 at 11:56











          • Great stuff! Cheers for pointing me in the right direction

            – Jim
            Nov 14 '18 at 13:54

















          Cheers for replying Andre That's the path I started down, but I can't get my head around the \s+= bit, what it means, I am sure its a method of selecting whats in there, but it puzzled me, if you wouldn't mind explaining for future reference?

          – Jim
          Nov 14 '18 at 11:50







          Cheers for replying Andre That's the path I started down, but I can't get my head around the \s+= bit, what it means, I am sure its a method of selecting whats in there, but it puzzled me, if you wouldn't mind explaining for future reference?

          – Jim
          Nov 14 '18 at 11:50















          Hey, it's called regular expression. You can reference what the "bits" are doing on www.regex101.com for e.g.

          – Andre Elrico
          Nov 14 '18 at 11:56





          Hey, it's called regular expression. You can reference what the "bits" are doing on www.regex101.com for e.g.

          – Andre Elrico
          Nov 14 '18 at 11:56













          Great stuff! Cheers for pointing me in the right direction

          – Jim
          Nov 14 '18 at 13:54





          Great stuff! Cheers for pointing me in the right direction

          – Jim
          Nov 14 '18 at 13:54











          0














          Alternate method:



          # use the filename vs this embedded example
          station_info <- readLines(textConnection("Version = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"))


          base:



          as.list(sapply(
          strsplit(station_info, split = "[[:space:]]*=[[:space:]]*"),
          function(x) setNames(x[2], x[1])
          )) -> station_info

          str(station_info, 1)
          ## List of 6
          ## $ Version : chr "2.0"
          ## $ StationName : chr "STN67_P70"
          ## $ BeginTime : chr "2017-10-06.03:25:00"
          ## $ EndTime : chr "2017-10-06.03:55:00"
          ## $ IgnoreNo : chr "5000"
          ## $ PumpedVolume: chr "0"


          tidyverse:



          library(tidyverse)

          # use the filename vs this embedded example
          station_info <- readLines(textConnection("Version = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"))

          str_split(station_info, pattern = "[[:space:]]*=[[:space:]]*") %>%
          map(~set_names(.x[2], .x[1])) %>%
          flatten() %>%
          str(1)


          Wrapping the base version (to avoid dependencies) into a function so you can re-use it for other stations:



          read_station_metadata <- function(path) {

          path <- path.expand(path)
          stopifnot(file.exists(path))

          station_info <- readLines(path, warn = FALSE)

          as.list(sapply(
          strsplit(station_info, split = "[[:space:]]*=[[:space:]]*"),
          function(x) setNames(x[2], x[1])
          ))

          }





          share|improve this answer
























          • Cheers for your detailed reply, my aim was to put it into a function eventually so that's ideal! Thanks, although I have just opened up a new can of worms. Many thanks

            – Jim
            Nov 14 '18 at 15:30


















          0














          Alternate method:



          # use the filename vs this embedded example
          station_info <- readLines(textConnection("Version = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"))


          base:



          as.list(sapply(
          strsplit(station_info, split = "[[:space:]]*=[[:space:]]*"),
          function(x) setNames(x[2], x[1])
          )) -> station_info

          str(station_info, 1)
          ## List of 6
          ## $ Version : chr "2.0"
          ## $ StationName : chr "STN67_P70"
          ## $ BeginTime : chr "2017-10-06.03:25:00"
          ## $ EndTime : chr "2017-10-06.03:55:00"
          ## $ IgnoreNo : chr "5000"
          ## $ PumpedVolume: chr "0"


          tidyverse:



          library(tidyverse)

          # use the filename vs this embedded example
          station_info <- readLines(textConnection("Version = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"))

          str_split(station_info, pattern = "[[:space:]]*=[[:space:]]*") %>%
          map(~set_names(.x[2], .x[1])) %>%
          flatten() %>%
          str(1)


          Wrapping the base version (to avoid dependencies) into a function so you can re-use it for other stations:



          read_station_metadata <- function(path) {

          path <- path.expand(path)
          stopifnot(file.exists(path))

          station_info <- readLines(path, warn = FALSE)

          as.list(sapply(
          strsplit(station_info, split = "[[:space:]]*=[[:space:]]*"),
          function(x) setNames(x[2], x[1])
          ))

          }





          share|improve this answer
























          • Cheers for your detailed reply, my aim was to put it into a function eventually so that's ideal! Thanks, although I have just opened up a new can of worms. Many thanks

            – Jim
            Nov 14 '18 at 15:30
















          0












          0








          0







          Alternate method:



          # use the filename vs this embedded example
          station_info <- readLines(textConnection("Version = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"))


          base:



          as.list(sapply(
          strsplit(station_info, split = "[[:space:]]*=[[:space:]]*"),
          function(x) setNames(x[2], x[1])
          )) -> station_info

          str(station_info, 1)
          ## List of 6
          ## $ Version : chr "2.0"
          ## $ StationName : chr "STN67_P70"
          ## $ BeginTime : chr "2017-10-06.03:25:00"
          ## $ EndTime : chr "2017-10-06.03:55:00"
          ## $ IgnoreNo : chr "5000"
          ## $ PumpedVolume: chr "0"


          tidyverse:



          library(tidyverse)

          # use the filename vs this embedded example
          station_info <- readLines(textConnection("Version = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"))

          str_split(station_info, pattern = "[[:space:]]*=[[:space:]]*") %>%
          map(~set_names(.x[2], .x[1])) %>%
          flatten() %>%
          str(1)


          Wrapping the base version (to avoid dependencies) into a function so you can re-use it for other stations:



          read_station_metadata <- function(path) {

          path <- path.expand(path)
          stopifnot(file.exists(path))

          station_info <- readLines(path, warn = FALSE)

          as.list(sapply(
          strsplit(station_info, split = "[[:space:]]*=[[:space:]]*"),
          function(x) setNames(x[2], x[1])
          ))

          }





          share|improve this answer













          Alternate method:



          # use the filename vs this embedded example
          station_info <- readLines(textConnection("Version = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"))


          base:



          as.list(sapply(
          strsplit(station_info, split = "[[:space:]]*=[[:space:]]*"),
          function(x) setNames(x[2], x[1])
          )) -> station_info

          str(station_info, 1)
          ## List of 6
          ## $ Version : chr "2.0"
          ## $ StationName : chr "STN67_P70"
          ## $ BeginTime : chr "2017-10-06.03:25:00"
          ## $ EndTime : chr "2017-10-06.03:55:00"
          ## $ IgnoreNo : chr "5000"
          ## $ PumpedVolume: chr "0"


          tidyverse:



          library(tidyverse)

          # use the filename vs this embedded example
          station_info <- readLines(textConnection("Version = 2.0
          StationName = STN67_P70
          BeginTime = 2017-10-06.03:25:00
          EndTime = 2017-10-06.03:55:00
          IgnoreNo = 5000
          PumpedVolume = 0"))

          str_split(station_info, pattern = "[[:space:]]*=[[:space:]]*") %>%
          map(~set_names(.x[2], .x[1])) %>%
          flatten() %>%
          str(1)


          Wrapping the base version (to avoid dependencies) into a function so you can re-use it for other stations:



          read_station_metadata <- function(path) {

          path <- path.expand(path)
          stopifnot(file.exists(path))

          station_info <- readLines(path, warn = FALSE)

          as.list(sapply(
          strsplit(station_info, split = "[[:space:]]*=[[:space:]]*"),
          function(x) setNames(x[2], x[1])
          ))

          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 14:21









          hrbrmstrhrbrmstr

          60.9k688150




          60.9k688150













          • Cheers for your detailed reply, my aim was to put it into a function eventually so that's ideal! Thanks, although I have just opened up a new can of worms. Many thanks

            – Jim
            Nov 14 '18 at 15:30





















          • Cheers for your detailed reply, my aim was to put it into a function eventually so that's ideal! Thanks, although I have just opened up a new can of worms. Many thanks

            – Jim
            Nov 14 '18 at 15:30



















          Cheers for your detailed reply, my aim was to put it into a function eventually so that's ideal! Thanks, although I have just opened up a new can of worms. Many thanks

          – Jim
          Nov 14 '18 at 15:30







          Cheers for your detailed reply, my aim was to put it into a function eventually so that's ideal! Thanks, although I have just opened up a new can of worms. Many thanks

          – Jim
          Nov 14 '18 at 15:30




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297441%2fextracting-a-few-bits-of-data-from-txt-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma