读取包含多个ID的数据文件并保存为不同的CSV文件。

huangapple go评论100阅读模式
英文:

reading data file containing multiple ID's into different csvs

问题

以下是您要翻译的内容:

给定以下数据结构的文件:

  1. FIXED=0
  2. LINES=1
  3. POINTS=5
  4. 390 397
  5. 390 396
  6. 389 395
  7. 389 394
  8. 388 393
  9. IMAGE=Name1.jpg
  10. ID=1
  11. FIXED=0
  12. LINES=1
  13. POINTS=4
  14. 255 503
  15. 256 502
  16. 256 501
  17. 256 500
  18. IMAGE=Name2.jpg
  19. ID=2
  20. FIXED=0
  21. LINES=1
  22. POINTS=6
  23. 262 431
  24. 262 430
  25. 262 429
  26. 262 428
  27. 262 427
  28. 262 426
  29. IMAGE=Name3.jpg
  30. ID=3

其中:

  • FIXEDID 之间的行属于一个个体
  • 数字表示两列变量

我们如何读取数据,然后转换为单独的 .csv 文件,其中:

  • 每个 .csv 的名称是 IMAGE= 后面的名称,即 Name1, Name2, Name3...
  • Name1.csv 的第一列数据是数字的第一列(390 390 389 389 388
  • Name1.csv 的第二列数据是数字的第二列(397 396 395 394 393
  • 对于 Name2.csvName3.csv 等都是相同的方式
  • FIXED=0LINES=1POINTS=5ID=1 可以忽略不计

请注意,POINTSIMAGE 之间的行数不固定。

英文:

Given a file with the following data structure:

  1. FIXED=0
  2. LINES=1
  3. POINTS=5
  4. 390 397
  5. 390 396
  6. 389 395
  7. 389 394
  8. 388 393
  9. IMAGE=Name1.jpg
  10. ID=1
  11. FIXED=0
  12. LINES=1
  13. POINTS=4
  14. 255 503
  15. 256 502
  16. 256 501
  17. 256 500
  18. IMAGE=Name2.jpg
  19. ID=2
  20. FIXED=0
  21. LINES=1
  22. POINTS=6
  23. 262 431
  24. 262 430
  25. 262 429
  26. 262 428
  27. 262 427
  28. 262 426
  29. IMAGE=Name3.jpg
  30. ID=3

Were:

  • The lines between FIXED and ID belong to an individual
  • The numbers represent two columns of variables

How would we read in the data and then transform into individual .csv files were:

  • The name of each .csv is the line after IMAGE= Name1, Name2, Name3...
  • First column of data of Name1.csv is the first column of numbers (390 390 389 389 388)
  • Second column of data of Name1.csv is the second column of numbers (397 396 395 394 393)
  • The same for Name2.csv, Name3.csv....
  • FIXED=0, LINES=1, POINTS=5, ID=1 can be dispensed

Please note that the number of rows between POINTS and IMAGE is not contant

答案1

得分: 1

这是你可以尝试的方法:

  1. library(stringr)
  2. # 从文件中读取数据
  3. data <- readLines("your_file.txt")
  4. # 初始化变量
  5. current_individual <- NULL
  6. current_points <- NULL
  7. current_data <- NULL
  8. # 处理数据的每一行
  9. for (line in data) {
  10. # 检查行是否以"IMAGE="开头
  11. if (str_starts(line, "IMAGE=")) {
  12. # 从行中提取个体名称
  13. individual_name <- str_remove(line, "IMAGE=")
  14. individual_name <- str_remove(individual_name, ".jpg")
  15. # 如果存在数据,将其保存到CSV文件中
  16. if (!is.null(current_individual) && !is.null(current_data)) {
  17. csv_file <- paste0(current_individual, ".csv")
  18. write.csv(current_data, file = csv_file, row.names = FALSE)
  19. }
  20. # 初始化新个体的变量
  21. current_individual <- individual_name
  22. current_points <- NULL
  23. current_data <- NULL
  24. } else if (str_starts(line, "POINTS=")) {
  25. # 从行中提取点数
  26. num_points <- as.numeric(str_remove(line, "POINTS="))
  27. # 初始化点数的变量
  28. current_points <- num_points
  29. current_data <- matrix(nrow = num_points, ncol = 2)
  30. } else if (str_detect(line, "\\d+ \\d+")) {
  31. # 从行中提取两个数字
  32. numbers <- str_split(line, " ")[[1]]
  33. # 将数字添加到当前数据中
  34. current_data <- rbind(current_data, as.numeric(numbers))
  35. }
  36. }
  37. # 将最后一个个体的数据保存到CSV文件中
  38. if (!is.null(current_individual) && !is.null(current_data)) {
  39. csv_file <- paste0(current_individual, ".csv")
  40. write.csv(current_data, file = csv_file, row.names = FALSE)
  41. }

这是你提供的R代码的翻译部分。

英文:

You could try this method:

  1. library(stringr)
  2. # Read the data from file
  3. data &lt;- readLines(&quot;your_file.txt&quot;)
  4. # Initialize variables
  5. current_individual &lt;- NULL
  6. current_points &lt;- NULL
  7. current_data &lt;- NULL
  8. # Process each line of the data
  9. for (line in data) {
  10. # Check if the line starts with &quot;IMAGE=&quot;
  11. if (str_starts(line, &quot;IMAGE=&quot;)) {
  12. # Extract the individual name from the line
  13. individual_name &lt;- str_remove(line, &quot;IMAGE=&quot;)
  14. individual_name &lt;- str_remove(individual_name, &quot;.jpg&quot;)
  15. # If there is existing data, save it to a CSV file
  16. if (!is.null(current_individual) &amp;&amp; !is.null(current_data)) {
  17. csv_file &lt;- paste0(current_individual, &quot;.csv&quot;)
  18. write.csv(current_data, file = csv_file, row.names = FALSE)
  19. }
  20. # Initialize variables for the new individual
  21. current_individual &lt;- individual_name
  22. current_points &lt;- NULL
  23. current_data &lt;- NULL
  24. } else if (str_starts(line, &quot;POINTS=&quot;)) {
  25. # Extract the number of points from the line
  26. num_points &lt;- as.numeric(str_remove(line, &quot;POINTS=&quot;))
  27. # Initialize variables for the points
  28. current_points &lt;- num_points
  29. current_data &lt;- matrix(nrow = num_points, ncol = 2)
  30. } else if (str_detect(line, &quot;\\d+ \\d+&quot;)) {
  31. # Extract the two numbers from the line
  32. numbers &lt;- str_split(line, &quot; &quot;)[[1]]
  33. # Append the numbers to the current data
  34. current_data &lt;- rbind(current_data, as.numeric(numbers))
  35. }
  36. }
  37. # Save the last individual&#39;s data to a CSV file
  38. if (!is.null(current_individual) &amp;&amp; !is.null(current_data)) {
  39. csv_file &lt;- paste0(current_individual, &quot;.csv&quot;)
  40. write.csv(current_data, file = csv_file, row.names = FALSE)
  41. }

huangapple
  • 本文由 发表于 2023年5月17日 17:51:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定