Python - 解析、分割文本并将其分成单独的行

标签 python csv parsing text split

我有一个文本文件,其中包含我想要导入到 Access 数据库中的数据。该文本文件包含一些我想放在一行中的段落。我已经用“@@@”分隔了我想要的每一行

这是我所拥有的示例:

@@@ 我想去学校,因为它很有趣。巴拉巴拉巴拉巴拉。我今天玩得很开心。 @@@我无缘无故地高兴。巴拉巴拉巴拉巴拉巴拉巴拉。我今天玩得很开心。

我希望它看起来像这样:

ID | Reporttext

1 | I would like to go to school because it's so much fun. Blah Blah Blah Blah. I am having so much fun today.

2 | I am so happy for no reason. Blah Blah Blah Blah Blah. I am having so much fun today.

但是,我知道我的代码很接近,但我得到了这个:

ID | Reporttext

1 | I would like to go to school because it's so much fun. Blah Blah Blah Blah.

2 | I am having so much fun today.

3 | I am so happy for no reason. Blah Blah Blah Blah Blah. I am having so much

4 | I am having so much fun today.

我尝试使用 IF 语句仅在行中存在“@@@”时添加 ID,但我无法让它工作。如果我这样做了,我认为它应该有效。我有 ID 和报告文本,使用分号作为分隔符。

这是我的代码:

import csv

with open("by2.txt") as txt, open('theoutput2.txt', 'a') as csvfile:
    writer = csv.writer(csvfile, delimiter=';')
    writer.writerow(('ID', 'Reporttext'))
    Id = 1
    for line in txt:
        words = line.strip().split("@@@")
        for word in words:
            writer.writerow((id, word.strip()))
            id += 1

最佳答案

您可以将 split("@@@")enumerate(iterable,start_index) 与生成器表达式结合使用:

t = """@@@ I would like to go to school because it's so much fun. Blah Blah Blah Blah. I am having so much fun today. @@@ I am so happy for no reason. Blah Blah Blah Blah Blah. I am having so much fun today."""

# split and enumerate(starting at 1)
# the if conditional inside the generator expression eleminates empty lines  
data = list(enumerate( (x.strip() for x in t.split("@@@") if x.strip()), 1))

print(data)
print("")

import csv
with open("t.txt", "w", newline = "") as csvfile:
    writer = csv.writer(csvfile, delimiter=';')
    writer.writerow(('ID', 'Reporttext'))
    writer.writerows(data)

print( open("t.txt").read())

输出:

# data
[(1, "I would like to go to school because it's so much fun. Blah Blah Blah Blah. I am having so much fun today."), 
 (2, 'I am so happy for no reason. Blah Blah Blah Blah Blah. I am having so much fun today.')]


# file
ID;Reporttext
1;I would like to go to school because it's so much fun. Blah Blah Blah Blah. I am having so much fun today.
2;I am so happy for no reason. Blah Blah Blah Blah Blah. I am having so much fun today.

多库:

关于Python - 解析、分割文本并将其分成单独的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55188884/

相关文章:

Python正则表达式按数字在线分割

javascript - Leaflet - 将功能添加到 json 对象并将结果放在 map 上

perl - 解析 CSV 文件和散列

java - 如何读取csv文件中的指定行

ruby - 如何将 .xml 文件转换为 ruby​​ 数组的实例?

javascript - Javascript 中的 IP 地址解析器

parsing - 为什么解释/脚本语言很少有多行注释?

python - 具有多个键值到数据框的字典

python - Pandas 相当于 "Select x from y groupby x"?

python - 类型错误 : 'str' does not support the buffer interface in 3. 4.1