python找出文本的位置和替换_使用Python在文本文件中查找和替换

I have a text file which is about 400,000 lines long. I need to import this text file into a program which only accepts text files which are delimited with spaces or tabs, but this text file is delimited with semi-colons. There is no option in the program I am exporting the text file from (Arcmap) to change the delimination and doing find and replace in the text file itself will literally take 2 days.

I have searched for a script to do this but they all seem to replace the whole LINE of the word file with a space, instead of individually replacing each semi-colon, Leaving me with an empty text file.

Here is a sample of my text file:

"OID_";"POINTID";"GRID_CODE";"POINT_X";"POINT_Y"

;1;-56.000000;200900.250122;514999.750122

;2;-56.000000;200900.750122;514999.750122

;3;-56.000000;200901.250122;514999.750122

;4;-57.000000;200901.750122;514999.750122

;5;-57.000000;200902.250122;514999.750122

;6;-57.000000;200902.750122;514999.750122

;7;-57.000000;200903.250122;514999.750122

;8;-57.000000;200903.750122;514999.750122

;9;-57.000000;200904.250122;514999.750122

;10;-57.000000;200904.750122;514999.750122

I need it to look something like this:

1 -56.000000 200900.250122 514999.750122

2 -56.000000 200900.750122 514999.750122

解决方案

How about this:

sed -i 's/;/ /g' yourBigFile.txt

This is not a Python solution. You have to start this in a shell. But if you use Notepad, I guess you are on Windows. So here a Python solution:

f1 = open('yourBigFile.txt', 'r')

f2 = open('yourBigFile.txt.tmp', 'w')

for line in f1:

f2.write(line.replace(';', ' '))

f1.close()

f2.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用正则表达式来解决这个问题。例如: ``` import re string = "Rte_Call_PpDiag_Service_0x01_MID2BSW_GetData" match = re.search(r"GetData$", string) if match: print(match.group()) ``` 这段代码会在字符串搜索以"GetData"结尾的子串,如果找到了,就会输出"GetData"。 ### 回答2: 使用Python将Rte_Call_PpDiag_Service_0x01_MID2BSW_GetData的GetData取出来,可以采用正则表达式来实现。 首先,导入re模块: import re 然后,定义一个用于匹配的正则表达式模式: pattern = r'Rte_Call_PpDiag_Service_0x01_MID2BSW_GetData\.GetData\(.*?\)' 接下来,假设我们有一个文本内容存储在变量text,通过使用re.findall()方法来查找所有匹配的结果: result = re.findall(pattern, text) 最后,得到的结果是一个包含所有匹配到的字符串的列表,取出GetData即可: getData = result[0] 最终的代码示例如下: import re pattern = r'Rte_Call_PpDiag_Service_0x01_MID2BSW_GetData\.GetData\(.*?\)' text = "Rte_Call_PpDiag_Service_0x01_MID2BSW_GetData.GetData(123)" result = re.findall(pattern, text) getData = result[0] print(getData) 运行以上代码,输出结果为: GetData(123) 通过以上步骤,我们成功将Rte_Call_PpDiag_Service_0x01_MID2BSW_GetData的GetData取出来。 ### 回答3: 要使用Python将Rte_Call_PpDiag_Service_0x01_MID2BSW_GetData的GetData取出来,可以通过以下方法实现: 1. 首先,假设Rte_Call_PpDiag_Service_0x01_MID2BSW_GetData是一个函数或方法的调用表达式。将它保存在一个变量,让我们称之为rte_call。 2. 使用rte_call.find('GetData')方法找到字符串"GetData"在rte_call位置。这将返回一个整数值,表示"GetData"在rte_call的起始索引。 3. 使用切片操作符(例如,rte_call[start_index:])从rte_call提取出"GetData"之后的部分。 具体的Python代码如下所示: ```python rte_call = "Rte_Call_PpDiag_Service_0x01_MID2BSW_GetData" start_index = rte_call.find('GetData') if start_index != -1: get_data = rte_call[start_index:] print(get_data) else: print("未找到GetData") ``` 请注意,上述代码使用的rte_call只是假设的一个表达式,实际上,你需要将其替换为你所要处理的具体字符串。此外,如果在rte_call找不到"GetData",代码将输出"未找到GetData"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值