正在更改 python 中文本文件的格式
Altering formats from text file in python
我正在尝试遍历如下所示的文本文件:
Funny Dogs | funny_dogs_video_id | #dog , #animal
Amazing Cats | amazing_cats_video_id | #cat , #animal
Another Cat Video | another_cat_video_id | #cat , #animal
Life at Google | life_at_google_video_id | #google , #career
Video about nothing | nothing_video_id |
我需要能够将文件打印成如下所示:
Funny Dogs (funny_dogs_video_id) [#dog , #animal]
Amazing Cats ( amazing_cats_video_id ) [#cat , #animal]
Another Cat Video ( another_cat_video_id ) [ #cat , #animal]
Life at Google (life_at_google_video_id) [#google , #career]
Video about nothing (nothing_video_id) []
我需要以“title(video_id)[tag]”格式列出所有可用视频
到目前为止我的代码:
def show_all_videos(self):
print("Here's a list of all available videos: ")
with open('/Users/Name/code-sample/python/src/videos.txt')as f:
lines = f.readlines()
lines.sort()
available_videos = ""
for line in lines:
available_videos += line
print(available_videos)
但是,我的代码以与文本文件已经存在的格式相同的格式输出我的文件。
有什么建议可以将此代码的格式更改为我想要的格式吗?
刚刚在 |
分手
也许和
一样简单
with open("videos.txt") as fh_src, open(destination, "w") as fh_dest:
for line in fh_src:
title, video_id, tags = line.split("|")
# rewrite line here, such as calling .strip() on the fields
fh_dest.write(f"{title} ({video_id}) [{tags}]\n")
尝试:
with open("videos.txt") as f:
lines = [l.strip() for l in f.readlines()]
available_videos = ""
for line in lines:
title, video, tags = [x.strip() for x in line.split("|")]
available_videos += f"{title} ({video}) [{tags}]\n"
>>> print(available_videos)
Funny Dogs (funny_dogs_video_id) [#dog , #animal]
Amazing Cats (amazing_cats_video_id) [#cat , #animal]
Another Cat Video (another_cat_video_id) [#cat , #animal]
Life at Google (life_at_google_video_id) [#google , #career]
Video about nothing (nothing_video_id) []
在 '|'
处拆分您的行并根据需要打印数据。
for line in lines:
title, video_id, tag = line.split('|')
print(f'{title} ({video_id}) [{tag}]')
我正在尝试遍历如下所示的文本文件:
Funny Dogs | funny_dogs_video_id | #dog , #animal
Amazing Cats | amazing_cats_video_id | #cat , #animal
Another Cat Video | another_cat_video_id | #cat , #animal
Life at Google | life_at_google_video_id | #google , #career
Video about nothing | nothing_video_id |
我需要能够将文件打印成如下所示:
Funny Dogs (funny_dogs_video_id) [#dog , #animal]
Amazing Cats ( amazing_cats_video_id ) [#cat , #animal]
Another Cat Video ( another_cat_video_id ) [ #cat , #animal]
Life at Google (life_at_google_video_id) [#google , #career]
Video about nothing (nothing_video_id) []
我需要以“title(video_id)[tag]”格式列出所有可用视频
到目前为止我的代码:
def show_all_videos(self):
print("Here's a list of all available videos: ")
with open('/Users/Name/code-sample/python/src/videos.txt')as f:
lines = f.readlines()
lines.sort()
available_videos = ""
for line in lines:
available_videos += line
print(available_videos)
但是,我的代码以与文本文件已经存在的格式相同的格式输出我的文件。 有什么建议可以将此代码的格式更改为我想要的格式吗?
刚刚在 |
也许和
一样简单with open("videos.txt") as fh_src, open(destination, "w") as fh_dest:
for line in fh_src:
title, video_id, tags = line.split("|")
# rewrite line here, such as calling .strip() on the fields
fh_dest.write(f"{title} ({video_id}) [{tags}]\n")
尝试:
with open("videos.txt") as f:
lines = [l.strip() for l in f.readlines()]
available_videos = ""
for line in lines:
title, video, tags = [x.strip() for x in line.split("|")]
available_videos += f"{title} ({video}) [{tags}]\n"
>>> print(available_videos)
Funny Dogs (funny_dogs_video_id) [#dog , #animal]
Amazing Cats (amazing_cats_video_id) [#cat , #animal]
Another Cat Video (another_cat_video_id) [#cat , #animal]
Life at Google (life_at_google_video_id) [#google , #career]
Video about nothing (nothing_video_id) []
在 '|'
处拆分您的行并根据需要打印数据。
for line in lines:
title, video_id, tag = line.split('|')
print(f'{title} ({video_id}) [{tag}]')