在 Ruby 中输入 CSV 的输出。控制台输出与文件输出不同

问题描述 投票:0回答:1

我正在学习ruby中的csv应用。

我有一个函数,它接受一个字符串并根据起始字母表查找单词

现在我正在尝试读取一个包含大量字符串的文件,程序应该读取它并在单独的 csv 文件中给出输出。

我的功能正在工作,csv也是如此。因为我可以在控制台中看到输出并且它是正确的..它只是不写入输出csv..而且我无法弄清楚为什么..

代码如下

require 'csv'

# Define a hash containing adjectives for each letter
adjectives_by_letter = {
  'a' => ['awesome', 'amazing', 'active'],
  'b' => ['beautiful', 'brave', 'bright'],
  'c' => ['calm', 'clever', 'creative'],
  'd' => ['daring', 'delightful', 'determined'],
  'e' => ['energetic', 'enthusiastic', 'elegant'],
  'f' => ['friendly', 'funny', 'fascinating'],
  'g' => ['gentle', 'generous', 'graceful'],
  'h' => ['happy', 'helpful', 'honest'],
  'i' => ['intelligent', 'inspiring', 'incredible'],
  'j' => ['joyful', 'jovial', 'judicious'],
  'k' => ['kind', 'keen', 'knowledgeable'],
  'l' => ['lovely', 'lively', 'loyal'],
  'm' => ['magical', 'merry', 'motivated'],
  'n' => ['nice', 'natural', 'neat'],
  'o' => ['optimistic', 'original', 'outgoing'],
  'p' => ['positive', 'playful', 'passionate'],
  'q' => ['quick', 'quiet', 'quirky'],
  'r' => ['radiant', 'reliable', 'resourceful'],
  's' => ['sincere', 'smart', 'strong'],
  't' => ['thoughtful', 'talented', 'trustworthy'],
  'u' => ['upbeat', 'unique', 'understanding'],
  'v' => ['vibrant', 'versatile', 'vivacious'],
  'w' => ['warm', 'wise', 'witty'],
  'x' => ['xenial', 'xenodochial', 'xenogenous'],
  'y' => ['youthful', 'yummy', 'yearning'],
  'z' => ['zesty', 'zealous', 'zen']
}

# Method to match each letter to a set of adjectives
def match_to_adjectives(letters, adjectives)
  matched_adjectives = {}

  letters.each do |letter|
    print "#{letter.upcase}: "
    available_adjectives = adjectives[letter.downcase] - matched_adjectives.values
    if available_adjectives.empty?
      puts "No available adjectives found for '#{letter}'"
    else
      adjective = available_adjectives.sample
      matched_adjectives[letter] = adjective
      puts adjective.upcase
    end
    puts "\n"
  end
end


# Define the input and output file paths
input_file = 'names.csv'
output_file = 'output.csv'

# Open the input file for reading
CSV.open(input_file, 'r') do |csv_in|
  # Open the output file for writing
  CSV.open(output_file, 'w') do |csv_out|
    # Iterate over each row in the input CSV
    csv_in.each do |row|
      # Extract the name from the row
      name = row[0]
      letters = name.chars
      results = match_to_adjectives(letters, adjectives_by_letter)
      
      # Write the name and its length to the output CSV
      csv_out << [ results ]
    end
  end
end

输入名字

喜力 斯特拉 工艺 百威啤酒 电晕

输出到控制台

H: HAPPY

E: ENTHUSIASTIC

I: INCREDIBLE

N: NATURAL

E: ELEGANT

K: KIND

E: ENTHUSIASTIC

N: NICE

S: SMART

T: THOUGHTFUL

E: ENTHUSIASTIC

L: LOYAL

L: LIVELY

A: ACTIVE

C: CLEVER

R: RESOURCEFUL

A: ACTIVE

F: FASCINATING

T: TALENTED

B: BRIGHT

U: UNIQUE

D: DETERMINED

W: WARM

E: ENTHUSIASTIC

I: INSPIRING

S: SMART

E: ELEGANT

R: RELIABLE

C: CREATIVE

O: OUTGOING

R: RADIANT

O: OPTIMISTIC

N: NICE

A: ACTIVE

[Finished in 634ms]

输出到output.csv

"[""h"", ""e"", ""i"", ""n"", ""e"", ""k"", ""e"", ""n""]"
"[""s"", ""t"", ""e"", ""l"", ""l"", ""a""]"
"[""c"", ""r"", ""a"", ""f"", ""t""]"
"[""b"", ""u"", ""d"", ""w"", ""e"", ""i"", ""s"", ""e"", ""r""]"
"[""c"", ""o"", ""r"", ""o"", ""n"", ""a""]"

我在 csv_out 中犯了什么错误?

ruby csv io
1个回答
0
投票

该函数将输出打印到控制台,但不返回外部代码可用于写入 CSV 文件的任何内容。 因此,当您调用 match_to_adjectives 时,它不会返回任何内容,只是将其打印到控制台

# Method to match each letter to a set of adjectives and return them
def match_to_adjectives(letters, adjectives)
  matched_adjectives = []
  letters.each do |letter|
    available_adjectives = adjectives[letter.downcase] || []
    if available_adjectives.empty?
      # Optionally, handle the case when no adjectives are found for a letter
      matched_adjectives << "No adjective for '#{letter}'"
    else
      adjective = available_adjectives.sample
      matched_adjectives << adjective
    end
  end
  matched_adjectives
end

# Define the input and output file paths
input_file = 'names.csv'
output_file = 'output.csv'

# Open the input file for reading
CSV.open(input_file, 'r') do |csv_in|
  # Open the output file for writing
  CSV.open(output_file, 'w') do |csv_out|
    # Iterate over each row in the input CSV
    csv_in.each do |row|
      # Extract the name from the row
      name = row[0]
      letters = name.chars
      results = match_to_adjectives(letters, adjectives_by_letter)
      
      csv_out << [name, results.join(', ')] # Adjust the join delimiter as needed
    end
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.