M
M
MVA1232020-06-18 11:09:59
Python
MVA123, 2020-06-18 11:09:59

How to get data from a line of a CSV file, and then write a specific text result to the same line?

There is a CSV file with data. You need to take the last value in the first line (before the comma), process it with an algorithm, write the resulting value after the comma to the same line. Then repeat the same with the next line. Do this with the first 100 lines, then with the second 100 lines.

Can this be done using the python standard library (csv module) or will I have to learn modules (pandas) additionally?

I apologize in advance for possibly stupid questions, I still have very little experience in programming and working with csv files. I would be very grateful if someone could tell me where to get the information.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-06-18
@MVA123

pandas is not needed.

import sys 
with open('tmp/new.csv') as fin: 
    w = csv.writer(sys.stdout) 
    for row in csv.reader(fin): 
        x = row[-1] 
        x2 = x*2 
        row.append(x2) 
        w.writerows([row])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question