-2

Hope you all are doing great. I have got a problem while writing some code. I have a CSV file with 2 columns. One is a mark (number between 1 and 5) and the other one contains the review that corresponds to it. My actual goal is to make a new CSV file, but modifying the note, which should be a 1 if the mark is >= 3 else 0. I'm actually stucked on this :

train_csv_path = "hotel_reviews.csv"
with open('new_hotel_reviews.csv', 'w') as newfile:
    writer = csv.writer(newfile)
    with open(train_csv_path, 'r') as f:
        reader = csv.reader(f)
        for row in reader:
            writer.writerow(row) 

It writes in the file, but I have a blank row between each row. Also I don't know how I'll access the first column to change it to 1 or 0.

The CSV file is semi-colon delimited.

a11apurva
  • 138
  • 10

2 Answers2

0

I would try it like this -

with open('outfile.csv', 'w', newline='') as outfile:
     writer=csv.writer(outfile, delimiter=';')     
     with open('infile.csv', mode='r') as infile:
          reader = csv.reader(infile, delimiter=';')
          headers = next(reader)
          writer.writerow(headers)
          for rows in reader:
               print(rows)
               if int(rows[0])>=3:
                    rows[0] = "1"
               else:
                    rows[0] = "0"
               writer.writerow(rows)

Add delimiter=';' in both reader and writer to take care of semi-colon.

newline='' in open() would take care of new lines being inserted between each rows.

This snippet would take care of headers too.

a11apurva
  • 138
  • 10
  • Hi, thanks for responding ! ive got this error : Traceback (most recent call last): File "C:/Users/Utilisateur/OneDrive - De Vinci/A4/Recherche/pythonProject1/main.py", line 13, in with open('new_hotel_reviews.csv', 'w', newline='') as outfile: PermissionError: [Errno 13] Permission denied: 'new_hotel_reviews.csv' I think that you cannot edit a row like this, we need to change it the moment we write in the new file – Un gars Comme ça Sep 13 '20 at 11:42
  • I tried this out and it works for me, and Permission Denied Error while writing a file can be due to some other reasons, [check this question](https://stackoverflow.com/questions/18529323/permission-denied-error-while-writing-to-a-file-in-python) – a11apurva Sep 13 '20 at 11:53
  • Also, your delimiter is not a comma but a semi-colon so change the delimiter in csv.reader to delimiter=';' – a11apurva Sep 13 '20 at 11:55
  • Ok so the pb was that the file was opened but i still have an error : – Un gars Comme ça Sep 13 '20 at 11:58
  • Ok so basically it works, but it combines my 2 columns into 1, so i have the mark 0 or 1 then a "coma" and then the review and i'd like it to be in 2 columns, one for the mark and the other one for the review, how should i do now ? – Un gars Comme ça Sep 13 '20 at 12:00
  • Change the 2nd line to writer=csv.writer(outfile, delimiter=';') – a11apurva Sep 13 '20 at 12:08
  • I have edited the above comment, note the change is in 'writer' – a11apurva Sep 13 '20 at 12:09
  • OOoooh it works !! Wow thanks a loooooooooooot. So the delimiter means that next to that, you go to the next column is that it ? (in the writer) – Un gars Comme ça Sep 13 '20 at 12:15
  • Note that you have to add delimiter=';' in writer and reader both, since your CVS file is delimited using a semi-colon instead of a comma. – a11apurva Sep 13 '20 at 12:16
  • Yes, CVS means 'Comma separated value', please accept the answer since it worked for you :) Also, please mention what your delimiter is from next time while asking a question about CSV files. :) – a11apurva Sep 13 '20 at 12:17
  • I didn' t even understand that ";" was my delimiter all along ahah, thanks, ive normally put your answer as resolved ! Thanks again – Un gars Comme ça Sep 13 '20 at 12:21
0

['4;Clean facility just off freeway ..... staff friendly and efficient .... good breakfast choices. We will stay here again for follow up visits with doctors at UT Northeast and recommend it for others requiring expected high standards from Hilton properties.']

this is a rows

4;Clean facility just off freeway ..... staff friendly and efficient .... good breakfast choices. We will stay here again for follow up visits with doctors at UT Northeast and recommend it for others requiring expected high standards from Hilton properties.

this is a rows[0], so changing rows[0] will result in an error, so rows[0][0] should be the way i guess