如何拆分文本文件并在 Python 中修改它? [英] how to split a text file and modify it in Python?

查看:31
本文介绍了如何拆分文本文件并在 Python 中修改它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个文本文件,内容如下:

101, Liberia, Monrovia, 111000, 3200000, Africa, English, Liberia Dollar;102,乌干达,坎帕拉,236000,34000000,非洲,英语和斯瓦希里语,乌干达先令;103, 马达加斯加, 塔那那利佛, 587000, 21000000, Africa, Magalasy and Frances, Malagasy Ariary;

我目前正在使用此代码打印文件:

 with open ("base.txt",'r') as f:对于 f 中的行:words = line.split(';')逐字逐句:打印(字)

我想知道的是,如何通过使用他们的 id 号(例如 101)修改一行并保持他们的格式并根据他们的 id 号添加或删除行?

解决方案

我的理解是你问如何修改一行中的一个单词,然后将修改后的行插入回文件中.

更改文件中的单词

def change_value(new_value, line_number, column):with open("base.txt",'r+') as f: #r+ 表示我们可以读写文件lines = f.read().split('\n') #lines 现在是文件中所有行的列表words = lines[line_number].split(',')词[列] = new_valuelines[line_number] = ','.join(words).rstrip('\n') #将行插入到每个单词由','分隔的行中f.seek(0)f.write('\n'.join(lines)) #将我们的新行写回文件

为了使用这个函数将line 3, word 2设置为Not_Madasgascar,这样调用:

change_word("Not_Madagascar", 2, 1)

您将始终必须将 1 添加到行/字号,因为第一行/字是 0

在文件中添加一个新行

def add_line(words, line_number):with open("base.txt",'r+') as f:行 = f.readlines()lines.insert(line_number, ','.join(words) + '\n')f.seek(0)f.writelines(行)

为了使用这个函数,在末尾添加一行包含单词 this line is atthe end 像这样调用它:

add_line(['this','line','is','at','the','end'], 4) #4是行号

有关打开文件的更多信息,请参见此处.>

有关读取和修改文件的更多信息,请参见此处.

I currently have a text file that reads like this:

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;

I'm currently printing the file using this code:

with open ("base.txt",'r') as f:
   for line in f:
      words = line.split(';')
      for word in words:
         print (word)

What I would like to know is, how can I modify a line by using their id number (101 for example) and keep the format they have and add or remove lines based on their id number?

My understanding your asking how to modify a word in a line and then insert the modified line back into the file.

Change a word in the file

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

In order to use this function to set line 3, word 2 to Not_Madasgascar call it like this:

change_word("Not_Madagascar", 2, 1)

You will always have to add 1 to the line/word number because the first line/word is 0

Add a new line to the file

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)

In order to use this function add a line at the end containing the words this line is at the end call it like this:

add_line(['this','line','is','at','the','end'], 4) #4 is the line number

For more information on opening files see here.

For more information on reading from and modifying files see here.

这篇关于如何拆分文本文件并在 Python 中修改它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆