簡體   English   中英

編輯每行文本文件的最后一個元素

[英]Edit last element of each line of text file

我有一個包含一些元素的文本文件;

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

這些列表的最后一個元素確定哪個用戶取出了給定的書。 如果為 0,則沒有人擁有它。 我想要一個代碼來將任何給定行的 ',0' 替換為輸入的 4 位數字,以顯示有人已取出這本書。

我已經使用 .replace 將它從 1828 更改為 0,但是我不知道如何將特定行的最后一個元素從 0 更改為其他內容。

由於工作/教育限制,我無法使用 csv,因此我必須將文件保留為 .txt 格式。

我也只能使用標准 python 庫,因此沒有熊貓。

您可以使用read_csvtxt捕獲到數據幀中:

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+')

這將第三本書的發行計數替換為 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  

評論后編輯:如果您只需要使用 python 標准庫,您可以使用文件讀取執行以下操作:

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)

文件內容

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

將此作為輸出

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']

你可以試試這個:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM