Python CSV File Read and Save

python read csv example
python write csv example
read csv file in python pandas
csv reader python
python read csv into list
python write list to csv
python read csv line by line
python csv write to specific column

I have a problem regarding to save newest seat update to my CSV file. I want every seat that is booked by user receive changes in CSV file file. Example : If user book A1 seat, the A1 is CSV file will replaced with X.

My program objective is to read csv file and store it, then make a replacement to it later. Every row have 5 seat. A1-A5 are first row of business class and B1-B5 are second row business class. I expect the program to replace the seat that is occupied/booked with "X". Example : A1 | X | A3 | A4 | A5 (A2 are not available, it is occupied)

Here is my code :

seat =[]
csvfile = open('coba.csv') 
seating = csv.reader(csvfile)
for line in seating:
    seat.append(line)

print("Buy seat ?")
answer_1 = input("Answer : ")
if (answer_1 == "yes"):
    answer_2 = input("Enter preferred seat: ")
    if (answer_2 == "A1"):
        row = 1
        column = 0
        seat[row][column] = "X"
        for line in seat:
            print(' | '.join(line))
        writer = csv.writer(open('coba.csv', 'w'))
        writer.writerows(line)

My CSV File :

[Business]
A1,A2,A3,A4,A5
B1,B2,B3,B4,B5
[Economy]
C1,C2,C3,C4,C5
D1,D2,D3,D4,D5
E1,E2,E3,E4,E5

Error :

PermissionError: [Errno 13] Permission denied: 'coba.csv'

Thanks in advance

I think you have typos. You should have checked len(line) instead of len(seating) and also using list instead of tuple with your seat

import csv

def load():
    seat = []
    csvfile = open('x.csv')
    seating = csv.reader(csvfile)
    for line in seating:
        if len(line) == 5:
            seat.append(line)
    print(seat)

load()

csv — CSV File Reading and Writing, The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,”  Python contains a module called csv for the handling of CSV files. The reader class from the module is used for reading data from a CSV file. The reader class from the module is used for reading data from a CSV file.

This gives the first 5 lines of column A:

from pandas import read_csv

data = read_csv('name.csv')

result = []

for i in range(5):
    result.append(data['A1'][i])

13.1. csv — CSV File Reading and Writing, Here's how to read a CSV using the DictWriter class. We first import the csv module and initialize an empty list results which we will use to store the data retrieved. We then define the reader object and use the csv. DictReader method to extract the data into the object. The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,” or “read data from this file which was generated by Excel,” without knowing the precise details of the CSV format used by Excel.

The problem that your first row is not valid as a row in the csv

(using pandas) This will do it:

import pandas as pd

df = pd.read_csv('coba.csv', header=None, skiprows=[0], sep=',')

print(df)

Or you can skip reading the first row in your code.

How to Read and Write CSV Files in Python, How do I read the last line of a csv file in Python? I have a problem regarding to save newest seat update to my CSV file. I want every seat that is booked by user receive changes in CSV file file. Example : If user book A1 seat, the A1 is CSV file will replaced with X. My program objective is to read csv file and store it, then make a replacement to it later. Every row have 5 seat.

Populate your seat list with lists of each row

seats = []
with open('file.csv', 'rb') as csvfile:
    csvreader = csv.reader(csvfile, delimiter=',')
    for row in csvreader:
        line = tuple(row)
        if len(line) == 5:
            seat.append(list(line))

Walk through each row in the list and change it to what you need to.

Reading and Writing CSV Files in Python, How do I read a row in a CSV file in Python? To read data from CSV files, you must use the reader function to generate a reader object. The reader function is developed to take each row of the file and make a list of all columns. Then, you have to choose the column you want the variable data for. It sounds a lot more intricate than it is.

How to read one single line of csv data in Python?, in the nrows parameter. Just drop your string data into a singleton list. The DictReader and DictWriter are classes available in Python for reading and writing to CSV. Although they are similar to the reader and writer functions, these classes use dictionary objects to read and write to csv files.

Reading and Writing CSV File using Python, To save this file as a CSV, click File->Save As, then in the Save As window, select "Comma Separated Values (.csv)" under the Format dropdown. Save it as​  The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,” or “read data from this file which was generated by Excel,” without knowing the precise details of the CSV format used by Excel.

Python CSV: Read and Write CSV files, The csv module in Python's standard library presents classes and methods to perform read/write operations on CSV files. writer(). This function in  Overview When you’re working with Python, you don’t need to import a library in order to read and write files. It’s handled natively in the language, albeit in a unique manner. The first thing you’ll need to do is use Python’s built-in open function to get a file object. The open function opens a file. […]

Reading and Writing CSV Files in Python using CSV Module , Many online services allow its users to export tabular data from the website into a CSV file. Files of CSV will open into Excel, and nearly all  How to read and write CSV files using Python. Please let me know your valuable feedback on the video by means of comments. Please like and share the video. Do not forget to subscribe to my channel

Comments
  • may i know why pandas?
  • I'm not quite sure what you want this program to do. It's failing because you can't call len() on a CSV Reader. What are you trying to append to the CSV file, and what is the condition you want to establish for appending?
  • what IS your question? What data do you want to extract? What is with the header line [Business], what are the A1-A5 and B1-B5 are for? What data do you need?
  • @Chirag reading in 3 lines of text - using a full fledged framework like pandas is simply overkill. Not all data has to be processed like a nail, even if you swing a pandas-hammer.
  • Update your question instead of posting in the comments.
  • @PatrickArtner by the way, the full csv file have business class and economy class header, so I think to read every line that contain 5 item only
  • Thanks for the response dude! I’ll try it, btw the seat is already list, little bit of typo hehe !!
  • @CtrlS umm i edited the thread, sorry im new to this platform
  • @DeltaTroz What thread did you edit? You haven't posted any new comments under your question. Also, you should be editing your question to include the new details you've mentioned in your comments.
  • @DeltaTroz I thought you were going to reply to PatrickArtner. Oh well.