【问题标题】:How to read and replace text in files in python line by line如何逐行读取和替换python文件中的文本
【发布时间】:2014-09-22 09:50:02
【问题描述】:

我是 python 初学者,想学习如何替换不同文件中的文本。

我知道该怎么做,但需要帮助:

我有 3 个文件 main.txtnames.txtnumber.txt

names.txt 看起来像这样:

Anna
Smith
Bob
Jhon

number.txt 看起来像这样:

1-522-223
1-523-232
1-593-573
1-322-242

filenames.txt 中的第 1 行对应 number.txt 中的第 1 行(所以 anna 的电话是 number.txt 中的第一个,Smith 的电话是 number.txt 中的第二个等等)

现在问题来了: 文件 main.txt 如下所示:

The person Judy lives in Ontario and has phone number 1-888-2923
The person Michael lives in Toronto and has phone number 1-999-2388
The person Cameron lives in Berlin and has phone number 1-666-2888
The person Douglas lives in Tokyo and has phone number 5-7777-223

我知道如何查找和替换,问题是我需要将 main.txt 中每一行的电话和姓名更改为 numbers.txtnames 中的相应行。 txt。 所以编辑后的main.txt应该是:

The person Anna lives in Ontario and has phone number 1-522-223
The person Smith lives in Toronto and has phone number 1-523-232
The person Bob lives in Berlin and has phone number 1-593-573
and so on...

我真的不知道该怎么做,而且文件很大,像 2000 行文本。谁能帮帮我?

【问题讨论】:

  • 你能把你写的代码贴出来吗?
  • 为什么要以三个文件中的信息开头?不同步太容易了,不妨考虑使用 CSV 样式的文件,其中包含不同列中的信息。

标签: python file text replace


【解决方案1】:

您可以一次压缩每个文件中的每一行、更新并再次写出。我为输出使用了一个新文件。

COL_NAME = 2
COL_PHONENUM = -1

with open('new_main.txt', 'w') as outfile:
    # zip corresponding lines from each file
    for entry in zip(open('names.txt'), open('number.txt'), open('main.txt')):
        main_data = entry[2].split()
        main_data[COL_NAME] = entry[0].strip()
        main_data[COL_PHONENUM] = entry[1].strip()
        outfile.write('{}\n'.format(' '.join(main_data)))

new_main.txt的内容:

$ cat new_main.txt
The person Anna lives in Ontario and has phone number 1-522-223
The person Smith lives in Toronto and has phone number 1-523-232
The person Bob lives in Berlin and has phone number 1-593-573
The person Jhon lives in Tokyo and has phone number 1-322-242

【讨论】:

  • 太棒了!对 zip 没有任何想法,只是阅读文档,这似乎是最好的方法,非常感谢!
【解决方案2】:

由于 number 是 main ([-1]) 中的最后一个方法和第 3 个中的 name ([2]),您可以拆分 main 中的行并替换 name 和 number:

with open('name.txt','r') as n:
  names =n.readlines()
  n.close()

with open('number.txt','r') as n:
  numbers =n.readlines()
  n.close()

with open('main.txt','r') as n:
  main =n.readlines()
  n.close()

newmain=[]
for i in main
    for j,k in zip(names,numbers):
         i.split()[2]=j
         i.split()[-1]=k
         newmain.append(i)

newmain=['',join(i) for i in newmain]

with open('main.txt','w') as n:
  main =n.write(str(newmain))
  n.close()

【讨论】:

  • 谢谢,我不知道 zip 的作用!
  • 它将它的迭代器参数压缩在一起! >>> l=[1,2,3]>>> k=['a','b','c'] >>> zip(l,k) [(1, 'a'), (2, 'b'), (3, 'c')] >>>
【解决方案3】:

所以你有 3 个数据集:

  • 名字
  • 城市
  • 数字

假设 3 个集合中每个项目之间的关系由它们的位置给出(#1 nae 与 #1 city 和 #1 phone 对应),你将不得不

  1. 从 main.txt 中提取城市列表(例如使用正则表达式)
  2. 组织您的数据(列表或字典)
  3. 使用模板字符串上的格式重新构建一个新的 main.txt

我们走吧:

def extractCities(path_to_main_txt_file):
    '''takes a path to txt file
    returns a list of cities'''

    import re

    with open(path_to_main_txt_file, 'r') as f:
        txt = f.read()

    return re.findall('in (.*) and', l)


def organizeData(names, cities, numbers):
    '''takes 3 lists
    returns 1 nested list'''

    return [[n, cities[names.index(n)], numbers[names.index(n)]] for n in names]

用法

>>> with open(r'path/to/names.txt') as f:
       names = f.read().splitlines()
>>> with open(r'path/to/numbers.txt') as f:
        numbers = f.read().splitlines()
>>> cities = extractCities(r'path/to/main.txt')
>>> data = organizeData(names, cities, numbers)
>>> template = u'The person {p} lives in {c} and has phone number {n}\n'
>>> main = [template.format(p=d[0], c=d[1], n=d[2]) for d in data]

现在main 包含一个字符串列表:您可以将其写入新文件,覆盖您的原始文件...

【讨论】:

  • 非常感谢您花时间写这篇文章,我非常感谢并从所有这些不同的方法中学到了很多东西。为您 +1 互联网!
猜你喜欢
  • 2015-11-23
  • 2015-05-21
  • 1970-01-01
  • 2020-11-12
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多