-1

This is my code to read my data from a CSV file row by row:

filepath = 'dataset-CalheirosMoroRita-2017.csv'
   with open(filepath) as fp:
   line = fp.readline()
   cnt = 1
while line:
   print("syntax {}: {}".format(cnt, line.strip())) 
   line = fp.readline()
   cnt += 1

I am trying to taking rows of CSV file as an input(string)type for my function

def members(dictArg, new_list,length_of_string):
  total=0

  for list_item in new_list:
      for letter, number in dictArg.items():

          if list_item==letter:

            print(letter)
            total= total+number
            print(total)

  return ((total/length_of_string)*100)

I don't know how to get multiple rows one by one in new_list, new_list is the argument which take input from csv file but this is confusing me how to get input from csv by using for loop .

Martin Evans
  • 45,791
  • 17
  • 81
  • 97

1 Answers1

0

The dataset that you are trying to parse appears to have hotel reviews. This file as you have seen, sometimes has one review split onto a number of lines. In this case, the review is enclosed in double quotes.

Python's CSV library can actually handle this automatically. As there is only one review per line, the csv.reader() delimiter can be changed to delimiter='\n'. The data though also contains a lot of whitespace which can also be removed using the strip() function.

For example, a list of all of the reviews could be created at follows:

import csv

reviews = []

with open('dataset-CalheirosMoroRita-2017.csv') as f_input:
    csv_input = csv.reader(f_input, delimiter='\n')
    header = next(csv_input)
    
    for row in csv_input:
        entry = [value.strip() for value in row if len(value.strip())]
        reviews.append(''.join(entry))

So the first 3 reviews could be displayed with print(reviews[:3]) giving:

['Everything from the weather, staff, food, property, fire pits, décor, spa, rooms and beach were top notch', 'The hotel it is fantastic built by the sea, living together with nature. Environment it is great as well as people and service.\nWe full enjoyed the place, and facilities.\nThanks for the "cidreira" and "madalenas" tea at reception', 'One dream! Cozy  and comfortable Hotel!  The best personalized one! All the employees are congratulated! Very nice taste, since reception to the end of the stay! We were welcome with tea and cake, as I have gluten aversion,, all the employees already knew and were waiting carefully. At night, we were received in the fire pits, with some wine and all the guests were invited to participate and interact.']
Martin Evans
  • 45,791
  • 17
  • 81
  • 97