Danilo
Danilo

Reputation: 23

Python how to add a column to a text file

I'm new to Python and I have a challenge. I need to add a column in a text file delimited by ";". So far so good ... except that the value of this column depends on the value of another column. I will leave an example in case I was not clear

My file looks like this:

Account;points
1;500
2;600
3;1500

If the value of the points column is greater than 1000, enter 2, if less, enter 1.

In this case the file would look like this:

Account;points;column_created
1;500;1
2;600;1
3;1500;2

Upvotes: 1

Views: 3248

Answers (3)

pk786
pk786

Reputation: 2400

Here is an approach without using pandas. This code assumes your points column will always be at the second position.

with open('stats.txt', 'r+') as file:
lines = file.readlines()
    file.seek(0,0)
    for line in lines:
        columns = line.strip().split(";")
        if int(columns[1])>1000:
           file.write(";".join(columns)+";2\n")
        else:
            file.write(";".join(columns) + ";1\n")

Upvotes: 1

fullstack
fullstack

Reputation: 21

You can use the Python 'csv' library to create CSV files. Here is the link to the documentation.

Upvotes: 0

furas
furas

Reputation: 142661

A file (hard drive) can't add a new item between new elements. You have to read all the data to memory, add a new column, and write all back to the file.

You could use Pandas to easily add a new column, based on the value from the other column.

In the example I use io.StringIO() only to create minimal working code, so everyone can copy it and the text. Use read_csv('input.csv', sep=';') with your file:

import pandas as pd
import io

text = '''Account;points
1;500
2;600
3;1500'''

#df = pd.read_csv('input.csv', sep=';')
df = pd.read_csv(io.StringIO(text), sep=';')

print('--- Before ---')
print(df)

df['column_created'] = df['points'].apply(lambda x: 2 if x > 1000 else 1)

print('--- After ---')
print(df) #

df.to_csv('output.csv', sep=';', index=False)

Result

--- Before ---
   Account  points
0        1     500
1        2     600
2        3    1500
--- After ---
   Account  points  column_created
0        1     500               1
1        2     600               1
2        3    1500               2

Upvotes: 2

Related Questions