Python 删除数据中特定字符串的最佳方法是什么?
有谁能帮助我以编程方式删除数据中的以下字符串?提前谢谢 我的文本文件:Python 删除数据中特定字符串的最佳方法是什么?,python,python-2.7,python-3.x,pandas,Python,Python 2.7,Python 3.x,Pandas,有谁能帮助我以编程方式删除数据中的以下字符串?提前谢谢 我的文本文件: -------------------------------------------- - All Data Monthly - -------------------------------------------- Local date-time is [2017-10-03 04:05:18.531] -------------------------------------
--------------------------------------------
- All Data Monthly -
--------------------------------------------
Local date-time is [2017-10-03 04:05:18.531]
--------------------------------------------
C NUMBER |SR LOCATION |COUNTY
1234 |SFO |IND
我想使用python程序删除数据中的以下字符串。字符串格式保持不变,但值会更改
--------------------------------------------
- All Data Monthly -
--------------------------------------------
Local date-time is [2017-10-03 04:05:18.531]
--------------------------------------------
请帮帮我。谢谢#一种简单的方法,假设不需要的头一次出现
# a simple approach that assumes the unwanted header occurs once at
# the top of the file.
with open('s.py') as f:
for i, line in enumerate(f):
if i < 10: continue
print line.rstrip() # or whatever
#文件的顶部。
以开放('s.py')作为f:
对于i,枚举(f)中的行:
如果i<10:继续
打印行.rstrip()#或其他内容
#一种简单的方法,假设不需要的头一次出现
#文件的顶部。
以开放('s.py')作为f:
对于i,枚举(f)中的行:
如果i<10:继续
打印行.rstrip()#或其他内容
首先,您需要找到字符串中要修剪的部分的索引,例如:
str1 = "this is string example....wow!!!"
str2 = "exam"
print str1.find(str2)
这将给你15,这是这个字符串中“考试”的索引
如果python在原始字符串中找不到该字符串,它将返回-1,因此使用If语句相应地采用您的代码
现在,您只需要通过这样做获得原始字符串的子字符串
subString = str1[:15]
将子字符串分配给新值“hello”
您可以通过在字符串中查找任何索引并修剪字符串的任何部分来执行相同的操作
original_string[start-index:end-index]
祝您好运首先,您需要找到字符串中要修剪的部分的索引,例如:
str1 = "this is string example....wow!!!"
str2 = "exam"
print str1.find(str2)
import datetime as dt
lines=[]
#Open testfile.txt and append all the content into a list
with open("testfile.txt", "r") as file:
for line in file:
#To remove the '\n' at the end of every line
lines.append(line.replace('\n', '')
#slicing the list containing the contents of 'testfile.txt' so that only the required stuff remains.
lines=lines[:9]
#I just added this to change the "local date-time is..." line and put the current date-time.
lines[6]=("Local date-time is [{}]".format(dt.datetime.now()))
#Finally, write all of the content into testfile.txt
with open("testfile.txt", "w") as file:
for line in lines:
file.write(str(line)+'\n')
这将给你15,这是这个字符串中“考试”的索引
如果python在原始字符串中找不到该字符串,它将返回-1,因此使用If语句相应地采用您的代码
现在,您只需要通过这样做获得原始字符串的子字符串
subString = str1[:15]
将子字符串分配给新值“hello”
您可以通过在字符串中查找任何索引并修剪字符串的任何部分来执行相同的操作
original_string[start-index:end-index]
祝你好运
import datetime as dt
lines=[]
#Open testfile.txt and append all the content into a list
with open("testfile.txt", "r") as file:
for line in file:
#To remove the '\n' at the end of every line
lines.append(line.replace('\n', '')
#slicing the list containing the contents of 'testfile.txt' so that only the required stuff remains.
lines=lines[:9]
#I just added this to change the "local date-time is..." line and put the current date-time.
lines[6]=("Local date-time is [{}]".format(dt.datetime.now()))
#Finally, write all of the content into testfile.txt
with open("testfile.txt", "w") as file:
for line in lines:
file.write(str(line)+'\n')
我希望注释行足以解释代码的作用。如果没有,请留言,我会尽力解释
我希望注释行足以解释代码的作用。如果没有,请留言,我会尽力解释。这在您的文件中只发生一次吗?这是你唯一一次看到一系列
----
字符吗?@birryrree每次我通过http从主站点下载数据时,我都会得到这些字符。所以每次你下载它时,你都会得到这个头出现一次的文件?如果是这样的话,你可以每次都修剪文本-最简单的方法是将字符串分解成一列行,然后迭代,直到你完成----…
@birryrree的第三行。谢谢,我将尝试循环并修剪它们。这在你的文件中只发生一次吗?这是你唯一一次看到一系列----
字符吗?@birryrree每次我通过http从主站点下载数据时,我都会得到这些字符。所以每次你下载它时,你都会得到这个头出现一次的文件?如果是这样的话,您可以每次都对文本进行裁剪-最简单的方法是将字符串拆分为一系列行,然后迭代,直到您完成----…
@birryrree的第三行。谢谢,我将尝试循环并裁剪它们