简体   繁体   English

将文件夹中的txt文件转换为csv文件中的行

[英]Convert txt files in a folder to rows in csv file

I have 100 txt files in a folder. 我的一个文件夹中有100个txt文件。 I would like to create a csv file in which the content of each text file becomes a single row (actually, a single cell in a row) in this csv file. 我想创建一个csv文件,其中每个文本文件的内容在此csv文件中变为一行(实际上是一行中的单个单元格)。 So, the result would be a csv file with 100 rows. 因此,结果将是具有100行的csv文件。

I tried the following code: 我尝试了以下代码:

import glob

read_files = glob.glob('neg/*')

with open("neg.csv", "wb") as outfile:
    for f in read_files:
        with open(f, "rb") as infile:
            for line in infile:
                outfile.write(line)

This create a csv with over thousands of rows since each txt file contains multiple paragraphs. 由于每个txt文件都包含多个段落,因此这会创建包含数千行的csv。 Any suggestion? 有什么建议吗?

Try: 尝试:

import glob
import csv

read_files = glob.glob('neg/*')

with open("neg.csv", "wb") as outfile:
    w=csv.writer(outfile)
    for f in read_files:
        with open(f, "rb") as infile:
            w.writerow([line for line in infile])

That makes each line a cell in the output and each file a row. 这使得每行输出中的一个单元格,每个文件一行。

If you want each cell to be the entire contents of the file, try: 如果希望每个单元格都是文件的全部内容,请尝试:

import glob
import csv

read_files = glob.glob('neg/*')

with open("neg.csv", "wb") as outfile:
    w=csv.writer(outfile)
    for f in read_files:
        with open(f, "rb") as infile:
            w.writerow(" ".join([line for line in infile]))

Before writing each line , first do line.replace('\\n',' ') to replace all new line characters with spaces. 在写每一line之前,首先要做line.replace('\\n',' ')用空格替换所有新行字符。

Obviously, adjust your newline character according to your OS. 显然,根据您的操作系统调整换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM