Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert csv to tsv
#1
I have converted a file from csv to tsv and get a KeyError when reading it using Python. So I want to ensure that the file was well converted. The code to convert from csv to tsv was;

import csv
csv.writer(open("../output.tsv", 'w+'), delimiter='\t').writerows(csv.reader(open("../AfricanPride7.csv")))
I am testing using only a few lines. The csv file had the following data;

Output:
Review LikedSituated in a vibrant gated community the Melrose Arch Hotel is a wonderful place to stoop for a day or more. The hotel staff were universally friendly the rooms were large and clean and they have an outstanding outdoor pool. complete with in-water furniture. Breakfast buffet was very complete with lots of choices to suit everyone. Also the bar in the hotel offers indoor and outdoor seating with wonderful cocktails.,1 This hotel was very nice and in a great location with shops and restaurants. We had a large king room that was quite spacious. Overall the room was nice and clean. I really don’t have any complaints but I do have one suggestion for improvement. The grout in the shower and toilet area could use some cleaning and/or re-grouting. It has stains/mildew in several areas. The lobby and dining areas are very nice and all of the staff we encountered were friendly and helpful.,1 The hotel has no gym. Breakfast was included in the room and I am a platinum member but the hotel does not serve eggs unless you pay extra. I have ordered a taxi talked to about 5 different people but after 40 min. they still could not get a taxi. I ended up finding my own ride. The hotel driver coming out of parking hit my back with side of the car. The hotel driver did not even come out of the car to apologize. The driver was more concerned about his bent side mirror as he drove off. Additionally there was a building construction right next to the hotel. The hotel did not mentioned it when I booked the room. They also gave me a room right next to the building being constructed. I did not realized it until the sound woke me up in the early morning.,0 My family chose this hotel for our 3 nights in Joburg when we were passing through to see other cities in SA. We liked that it was highly rated and in a safe area. We found the hotel to be outstanding in its decor and the staff were efficient and very nice.Unfortunately on our last night there we discovered a live scorpion about 3 inches long under the covers of our bed. Fortunately we were able to dispatch it before it could sting us. We were pretty upset and called the front desk. The night manager immediately came up to the room and offered to move us but we didn’t think going to another room would help anything. Instead the housekeepers changed our linens and we searched the room to be sure there were no other visitors. The photo is not the actual scorpion because we were so rattled that we tossed it in the toilet before we thought to show it to the staff.The staff were very understanding and professional as you would expect from a hotel of this quality. The Westin staff was polite but did not refund our night’s stay which we thought would have been appropriate for this unfortunate experience.We would stay here again but definitely would be checking our bed linens before getting in.,0 This hotel is very clean modern and stylish. I stayed here for a convention for a week and as a foreigner the location was perfect many very nice restaurants and the neighborhood is very safe. We had breakfast each morning in the hotel it was outstanding. Most important was the staff. I had a minor issue and the staff including the manager stepped in to correct the situation. In my opinion how you are treated when there is an issue is a sign of a good hotel. Well they were outstanding. I would recommend this hotel to anyone.,1
The TSV Output file contains;
Output:
Review LikedSituated in a vibrant gated community the Melrose Arch Hotel is a wonderful place to stoop for a day or more. The hotel staff were universally friendly the rooms were large and clean and they have an outstanding outdoor pool. complete with in-water furniture. Breakfast buffet was very complete with lots of choices to suit everyone. Also the bar in the hotel offers indoor and outdoor seating with wonderful cocktails. 1 This hotel was very nice and in a great location with shops and restaurants. We had a large king room that was quite spacious. Overall the room was nice and clean. I really don’t have any complaints but I do have one suggestion for improvement. The grout in the shower and toilet area could use some cleaning and/or re-grouting. It has stains/mildew in several areas. The lobby and dining areas are very nice and all of the staff we encountered were friendly and helpful. 1 The hotel has no gym. Breakfast was included in the room and I am a platinum member but the hotel does not serve eggs unless you pay extra. I have ordered a taxi talked to about 5 different people but after 40 min. they still could not get a taxi. I ended up finding my own ride. The hotel driver coming out of parking hit my back with side of the car. The hotel driver did not even come out of the car to apologize. The driver was more concerned about his bent side mirror as he drove off. Additionally there was a building construction right next to the hotel. The hotel did not mentioned it when I booked the room. They also gave me a room right next to the building being constructed. I did not realized it until the sound woke me up in the early morning. 0 We spent two nights at this Autograph Collection Hotel. The staff were friendly and welcoming. Excellent concierge services. We did not eat at the restaurant as there were so many good options within walking distance. There were also interesting and diverse shops nearby. We felt very safe exploring the area. The room was comfortable and well appointed and everything about it was first class. I would highly recommend this hotel! 1 As a seasoned hotel-goer (Marriott Ambassador / Lifetime Titanium) it's rare that a hotel brings joy to my heart. But this is a very special place.I'd loved it when travelling solo for work a year before. This time I brought my wife and eight-month-old baby along while I worked. And we all adored it.The staff are simply fantastic: a more motivated friendly team you couldn't hope to find - from the warmth of the ebullient General Manager down.And the hotel's design is simply gorgeous.Sure we had a couple of minor issues during our stay - sorted professionally and with kindness. But nothing that distracted from a memorable experience in a lovely and safe Jo'burg location. 1 We had a birthday celebration at the hotel for 20 guests. The hotel has a private lounge where they set up tables and serve guests who pre booked for the event. The food was great and we received excellent service at the hotel. 1 We enjoyed our five day stay very much. Excellent restaurant with excellent service. Funky bathtubs with rubber ducks you are encouraged to take with you. We did. . The linens were great and the area is secure and gated. Staff was pleasant. 1 My family chose this hotel for our 3 nights in Joburg when we were passing through to see other cities in SA. We liked that it was highly rated and in a safe area. We found the hotel to be outstanding in its decor and the staff were efficient and very nice.Unfortunately on our last night there we discovered a live scorpion about 3 inches long under the covers of our bed. Fortunately we were able to dispatch it before it could sting us. We were pretty upset and called the front desk. The night manager immediately came up to the room and offered to move us but we didn’t think going to another room would help anything. Instead the housekeepers changed our linens and we searched the room to be sure there were no other visitors. The photo is not the actual scorpion because we were so rattled that we tossed it in the toilet before we thought to show it to the staff.The staff were very understanding and professional as you would expect from a hotel of this quality. The Westin staff was polite but did not refund our night’s stay which we thought would have been appropriate for this unfortunate experience.We would stay here again but definitely would be checking our bed linens before getting in. 0 This hotel is very clean modern and stylish. I stayed here for a convention for a week and as a foreigner the location was perfect many very nice restaurants and the neighborhood is very safe. We had breakfast each morning in the hotel it was outstanding. Most important was the staff. I had a minor issue and the staff including the manager stepped in to correct the situation. In my opinion how you are treated when there is an issue is a sign of a good hotel. Well they were outstanding. I would recommend this hotel to anyone. 1
Is the conversion correct? Please assist
Reply
#2
Obviously the output has an extra blank line between rows. To fix this you need to open the output file with newline='' argument, e.g.
open("../output.tsv", 'w+', newline='')

Also the output has some extra reviews that is not clear from where they come

That said, better refactor your monstrous line csv.writer(open("../output.tsv", 'w+'), delimiter='\t').writerows(csv.reader(open("../AfricanPride7.csv"))) for readability
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I have re-written the code and not getting a KeyError on the newly converted tsv file

import csv

with open("../AfricanPride7.csv",'r', encoding='utf-8') as csvin, open("../output.tsv", 'w', newline='', encoding='utf-8') as tsvout:
    csvin = csv.reader(csvin)
    tsvout = csv.writer(tsvout, delimiter='\t')

    for row in csvin:
        tsvout.writerow(row)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020