简体   繁体   中英

Edit last element of each line of text file

I've got a text file with some elements as such;

0,The Hitchhiker's Guide to Python,Kenneth Reitz,2/4/2012,0
1,Harry Potter,JK Rowling,1/1/2010,8137
2,The Great Gatsby,F. Scott Fitzgerald,1/2/2010,0
3,To Kill a Mockingbird,Harper Lee,1/3/2010,1828

The last element of these lists determine which user has taken out the given book. If 0 then nobody has it. I want a code to replace the ',0' of any given line into an input 4 digit number to show someone has taken out the book.

I've used .replace to change it from 1828 eg into 0 however I don't know how I can change the last element of specific line from 0 to something else.

I cannot use csv due to work/education restrictions so I have to leave the file in .txt format.

I also can only use Standard python library, therefore no pandas.

You can capture txt using read_csv into a dataframe:

import pandas as pd


df = pd.read_csv("text_file.txt", header=None)
df.columns = ["Serial", "Book", "Author", "Date", "Issued"]
print(df)
df.loc[3, "Issued"] = 0
print(df)
df.to_csv('text_file.txt', header=None, index=None, sep=',', mode='w+')

This replaces the third book issued count to 0.

Serial                              Book               Author      Date  \
0       0  The Hitchhiker's Guide to Python        Kenneth Reitz  2/4/2012   
1       1                      Harry Potter           JK Rowling  1/1/2010   
2       2                  The Great Gatsby  F. Scott Fitzgerald  1/2/2010   
3       3             To Kill a Mockingbird           Harper Lee  1/3/2010   

   Issued  
0       0  
1    8137  
2       0  
3    1828  

   Serial                              Book               Author      Date  \
0       0  The Hitchhiker's Guide to Python        Kenneth Reitz  2/4/2012   
1       1                      Harry Potter           JK Rowling  1/1/2010   
2       2                  The Great Gatsby  F. Scott Fitzgerald  1/2/2010   
3       3             To Kill a Mockingbird           Harper Lee  1/3/2010   

   Issued  
0       0  
1    8137  
2       0  
3       0  

Edit after comment : In case you only need to use python standard libraries, you can do something like this with file read:

import fileinput

i = 0
a = 5 # line to change with 1 being the first line in the file
b = '8371'
to_write = []

with open("text_file.txt", "r") as file:
    for line in file:
        i += 1
        if (i == a):
            print('line before')
            print(line)
            line = line[:line.rfind(',')] + ',' + b + '\n'
            to_write.append(line)
            print('line after edit')
            print(line)
        else:
            to_write.append(line)
print(to_write)

with open("text_file.txt", "w") as f: 
    for line in to_write:
            f.write(line)

File content

0,The Hitchhiker's Guide to Python,Kenneth Reitz,2/4/2012,0
1,Harry Potter,JK Rowling,1/1/2010,8137
2,The Great Gatsby,F. Scott Fitzgerald,1/2/2010,84
3,To Kill a Mockingbird,Harper Lee,1/3/2010,7895
4,XYZ,Harper,1/3/2018,258
5,PQR,Lee,1/3/2019,16

gives this as output

line before
4,XYZ,Harper,1/3/2018,258

line after edit
4,XYZ,Harper,1/3/2018,8371

["0,The Hitchhiker's Guide to Python,Kenneth Reitz,2/4/2012,0\n", '1,Harry Potter,JK Rowling,1/1/2010,8137\n', '2,The Great Gatsby,F. Scott Fitzgerald,1/2/2010,84\n', '3,To Kill a Mockingbird,Harper Lee,1/3/2010,7895\n', '4,XYZ,Harper,1/3/2018,8371\n', '5,PQR,Lee,1/3/2019,16\n', '\n']

you can try this:

to_write = []
with open('read.txt') as f:  #read input file
    for line in f:
        if int(line[-2]) ==0:
            to_write.append(line.replace(line[-2],'1234'))  #any 4 digit number
        else:
            to_write.append(line)


with open('output.txt', 'w') as f:  #name of output file
    for _list in to_write:

            f.write(_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM