[英]how to split a text file and modify it in Python?
我目前有一個文本文件,內容如下:
101, Liberia, Monrovia, 111000, 3200000, Africa, English, Liberia Dollar;
102, Uganda, Kampala, 236000, 34000000, Africa, English and Swahili, Ugandan Shilling;
103, Madagascar, Antananarivo, 587000, 21000000, Africa, Magalasy and Frances, Malagasy Ariary;
我目前正在使用以下代碼打印文件:
with open ("base.txt",'r') as f:
for line in f:
words = line.split(';')
for word in words:
print (word)
我想知道的是,如何使用ID號(例如101)修改行並保持其格式,並根據ID號添加或刪除行?
我的理解是您詢問如何修改一行中的單詞,然后將修改后的行重新插入文件中。
def change_value(new_value, line_number, column):
with open("base.txt",'r+') as f: #r+ means we can read and write to the file
lines = f.read().split('\n') #lines is now a list of all the lines in the file
words = lines[line_number].split(',')
words[column] = new_value
lines[line_number] = ','.join(words).rstrip('\n') #inserts the line into lines where each word is seperated by a ','
f.seek(0)
f.write('\n'.join(lines)) #writes our new lines back into the file
為了使用此功能設置line 3, word 2
Not_Madasgascar
line 3, word 2
像這樣稱呼它:
change_word("Not_Madagascar", 2, 1)
您將始終必須在行/單詞號上添加1
,因為第一行/單詞是0
def add_line(words, line_number):
with open("base.txt",'r+') as f:
lines = f.readlines()
lines.insert(line_number, ','.join(words) + '\n')
f.seek(0)
f.writelines(lines)
為了使用該功能,在包含該單詞的末尾添加一行this
line
is
at
the
end
調用它是這樣的:
add_line(['this','line','is','at','the','end'], 4) #4 is the line number
有關打開文件的更多信息,請參見此處 。
有關讀取和修改文件的更多信息,請參見此處 。
pandas
是解決您的需求的強大工具。 它提供了輕松處理CSV文件的工具。 您可以在DataFrames
管理數據。
import pandas as pd
# read the CSV file into DataFrame
df = pd.read_csv('file.csv', sep=',', header=None, index_col = 0)
print (df)
# eliminating the `;` character
df[7] = df[7].map(lambda x: str(x).rstrip(';'))
print (df)
# eliminating the #101 row of data
df.drop(101, axis=0, inplace=True)
print (df)
如果您嘗試保留原始文件的順序並能夠引用文件中的行以進行修改/添加/刪除,則將該文件讀入OrderedDict
可能會有所幫助。 在下面的示例中,關於文件的完整格式有很多假設,但是對於您的測試用例將起作用:
from collections import OrderedDict
content = OrderedDict()
with open('base.txt', 'r') as f:
for line in f:
if line.strip():
print line
words = line.split(',') # Assuming that you meant ',' vs ';' to split the line into words
content[int(words[0])] = ','.join(words[1:])
print(content[101]) # Prints " Liberia, Monrovia, etc"...
content.pop(101, None) # Remove line w/ 101 as the "id"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.