语言:python(版本:3.7.3) 熟练程度:新手
我想删除文件每一行中的所有前导和尾随空格。因此,我使用了内置方法“ strip()”,该方法几乎可以完成工作,但是我的代码从文件底部开始不活动,这导致文件中的行重复。任何帮助都将有助于实现这一目标。
我的代码如下:
class filecorrection:
def removeLeadingandTrailingSpacesinaFile(self, fromFile):
with open(fromFile, 'r+') as file:
lines = file.readlines()
for line in lines:
file.write(line.strip() + "\n")
cm = filecorrection()
cm.removeLeadingandTrailingSpacesinaFile("filepath")
我试图使用seek方法将指针带到文件的开头,但没有得到完美的输出。
class filecorrection:
def removeLeadingandTrailingSpacesinaFile(self, fromFile):
with open(fromFile, 'r+') as file:
lines = file.readlines()
**file.seek(0)**
for line in lines:
file.write(line.strip() + "\n")
cm = filecorrection()
cm.removeLeadingandTrailingSpacesinaFile("filepath")
期望 :(删除尾部和前导空格)
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>testing</publish_date>
</book>
实际:
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>testing</publish_date>
</book><book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>testing</publish_date>
</book>
答案 0 :(得分:1)
您可以像这样两次打开文件
fn main() {
let cache = Cache::new(Path::new("/tmp/cache.txt"));
let (lock_cache, _) = BiLock::new(cache);
let (tx, rx) = futures::sync::mpsc::unbounded::<Data>();
let fut = rx.for_each(|data: Data| {
lock_cache.lock().and_then(move |mut cache| {
poll_fn(move || {
tokio_threadpool::blocking(|| cache.save(data).unwrap())
.map_err(|_| panic!("the threadpool shut down"))
})
})
});
}
而不是尝试同时读取和写入文件。以写模式(class filecorrection:
def removeLeadingandTrailingSpacesinaFile(self, fromFile):
lines = []
with open(fromFile, 'r') as file:
lines = file.readlines()
with open(fromFile, "w") as file:
for line in lines:
file.write(line.strip() + "\n")
cm = filecorrection()
cm.removeLeadingandTrailingSpacesinaFile("test.xml")
)打开文件会将文件截断为零长度,并从头开始写。
答案 1 :(得分:0)
您的代码存在问题
也请尝试对读取的行和创建的新行使用不同的变量。这是一个好习惯。
请参阅下面的更新代码。
with open(fromFile, 'r+') as infile, \
open(writeFile, 'w') as outfile:
for line in infile:
new_line = line.rstrip('\n').strip()
#now write the new line to out file
outfile.write(new_line + "\n")
答案 2 :(得分:-1)
您可以使用另一个文件来存储中间结果,然后用它替换原始文件。在这里,您正在读写同一文件。
OAGTokensWOStop = []
for i in range(2708):
row = []
for tweet in OAG_Tokenized[i]:
if tweet not in stop_words:
row.append(tweet)
OAGTokensWOStop.append(row)