[Basic Python] Questions when you process Excel data with Python.

Asked 2 years ago, Updated 2 years ago, 14 views

Hello, I am a student who is learning Python basics at school.

I'm currently studying how to process Excel data (csv) with Python. It's hard to do it alone.

For example, an Excel file (only numbers are entered in 3 columns/ I don't know how to attach the file) is converted to -99999, and when you enter it, you open the file first, and then read the data as a readlines() function, but then it's blocked. I understand the conditions and things like that, but I'm curious because I don't know what to do with the file I read. And how to make the Excel file that has been processed since then...

O = open("test.csv","r")

raw = O.readlines()

O.close()

data=[ ]

It would be nice to follow an example sentence that processes Excel data once, but I'm asking you this because it's hard to find.

Excel file (csv)

Row 1, row 2, row 3, row

1         5           7    
2        -9           9 
7         15         98
           .
           .
           .
           .
           .

python

2022-09-22 12:26

1 Answers

Python has a good module called csv that processes csv files. I think you'd better use this.

import csv


intput_file = open("test.csv","r")
output_file = open('test_result.csv', 'w+')

reader = csv.reader(intput_file)
writer = csv.writer(output_file)
for row in reader:
    new_row = []
    for i in row:
        if int(i) < 0:
            new_row.append('-9999')
        else:
            new_row.append(i)
    writer.writerow(new_row)

If you study more about the following, you will be able to write your own code.

I hope we can meet again with a good question :)


2022-09-22 12:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.