|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 2830680393 于 2022-4-30 19:40 编辑
有如下的表格,请问怎么把content列的内容保存为对应的filename(如file01)命名的文件,并将该文件放在一个已经存在的label文件夹(如class1)中。
filename content label
file01 asdjhsgdjh class1
file02 ashdgkjhcb class2
file03 skhakhscvjh class3
……… ……………… ………
……… ……………… ………
……… ……………… ………
……… ……………… ………
比如将上述表格中content列的内容”ashdgkjhcb“ ,保存在一个名叫“file02“的txt文件中,并将”file02.txt“放在一个名叫”class2“的文件夹中。
本帖最后由 jackz007 于 2022-4-30 20:08 编辑
- import os
- fp1 = open('sample.txt')
- for x in fp1:
- e = x . split()
- if len(e) == 3:
- p = os . path . join(e[2] , e[0] + '.txt')
- with open(p , "wt") as fp2:
- fp2 . write(e[1] + '\n')
- fp1 . close()
复制代码
输入文件 "sample.txt"
- file01 asdjhsgdjh class1
- file02 ashdgkjhcb class2
- file03 skhakhscvjh class3
复制代码
代码目录内必须同时有名为 :
"class1"、"class2"、"class3" 的子目录。
|
|