我不是程序员,我一直在尽力用 Python 3.4 创建一些小脚本,帮助我完成工作中的不同任务。
我有几个 .txt文件,而且我需要追加文件中的每一行:
我设法为一个特定文件执行此操作,但我很难为该文件夹中的所有文件执行此操作。我试过import glob
但没有成功。
这是现在的代码(一团糟......部分有效):
with open('Ruth.txt', 'r') as program:
data = program.readlines()
with open('Ruth.txt', 'w') as program:
for (number, line) in enumerate(data):
program.write('%d","%s' % (number + 1, line))
files = 'Ruth.txt'
all_lines = []
for f in files.split():
lines = open(f, 'r').readlines()
for line in lines:
all_lines.append('"' + f + '"' + ',' + '"' + f + line.strip() + '"')
fout = open(f + 'out.csv', 'w')
fout.write('\n'.join(all_lines))
fout.close()
答案 0 :(得分:0)
试试这个:
import os
def add_numbers(filename):
with open(filename, 'r') as readfile:
data = readfile.readlines()
with open(filename, 'w') as writefile:
for i, line in enumerate(data):
writefile.write('%d. %s' % (i + 1, line))
for path, _, filenames in os.walk(folder):
for filename in filenames:
add_numbers(os.path.join(path, filename))
这将为目录中的每个文件和所有子目录中的每个文件添加数字。如果您不希望它检查所有子目录,请将for
循环更改为:
path, _, filenames = next(os.walk(folder))
for filename in filenames:
add_numbers(os.path.join(path, filename))
答案 1 :(得分:0)
这将获取当前文件夹中的所有文本文件,并将它们转换为utf-8编码的“csv-style”文件,以便文本中的空格变为逗号,文件名和行号也以逗号分隔。
from glob import glob
filenames = glob("*.txt")
text = ''
for fn in filenames:
with open(fn,'r') as f:
for i,line in enumerate(f):
line=','.join(line.split())
text += ','.join((line,fn,i+1)) + '\n'
fnew = fn.rsplit('.',1)[0]+'.csv'
with open(fnew,'w', encoding='utf-8') as f:
f.write(text)
答案 2 :(得分:0)
这里是一个完整的脚本,它接受一个位置参数(文件夹)并在与文件相同的级别创建一个新的.csv文件。
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from pathlib import Path
def get_files(folder_path, suffix=".txt"):
return Path(folder_path).glob("**/*%s" % suffix)
def write_lines(file_):
with file_.with_suffix(".csv").open("w") as fout, file_.open(encoding="utf-8") as fin:
for i, line in enumerate(fin, 1):
# line number, file name, line
new_line = ",".join(["%d." % i, file_.name, line])
fout.write(new_line)
def main(folder):
for file_ in get_files(folder):
print(file_)
write_lines(file_)
if __name__ == '__main__':
try:
main(sys.argv[1])
except IndexError:
print("usage: %s foldername" % sys.argv[0])