一些 csv 文件中的数据在一行中写入

Data being written in a single line in some csv file

我已经编写了一些代码来从 "pdf" 文件的特定页面读取数据并使用 python 将其写入 csv 文件。它只是部分地完成了它的工作。但是,当涉及到将数据写入 csv 文件时,它会将这些写入单行而不是常规模式。我应该如何修改我的脚本以达到目的?提前致谢。

这是我到目前为止尝试过的方法:

import csv
from PyPDF2 import PdfFileReader

outfile = open("conversion.csv",'w', newline='')
writer = csv.writer(outfile)

infile = open('some.pdf', 'rb')
reader = PdfFileReader(infile)
contents = reader.getPage(7).extractText().split('\n')
writer.writerow(contents)

print(contents)
infile.close()

pdf格式的数据是这样的:

Creating a PivotTable Report 162
PivotCaches 165
PivotTables Collection 165
PivotFields 166
CalculatedFields 170

我在 csv 输出中获取数据,例如:

Creating a PivotTable Report 162 PivotCaches 165 PivotTables Collection 165 PivotFields 166 CalculatedFields 170

对于此特定代码:

因为内容是项目列表[行]

contents = reader.getPage(7).extractText().split('\n')
for each in contents:
    writer.writerow(each)

print(contents)

试试这个然后告诉我。

假设你有

>>> print(s)
Line 1
Line 2
Line 3
Line 4

或该字符串的表示形式:

>>> s
'Line 1\nLine 2\nLine 3\nLine 4'

如果按 \n 拆分,行尾不再存在:

>>> s.split('\n')
['Line 1', 'Line 2', 'Line 3', 'Line 4']

因此,如果您依次将每一行打印到一个文件中,您将得到一行:

>>> with open('/tmp/file', 'w') as f:
...    for line in s.split('\n'):
...       f.write(line)
... 
# will write 'Line 1Line 2Line 3Line 4'

因此您需要在写入文件时添加行结尾:

writer.writerow('\n'.join(contents)) # assuming that is a list of strings

您还应该使用上下文管理器(我在上面使用的 with)或关闭文件,否则您可能只会得到部分写入。

这是我想要的解决方案:

import csv
from PyPDF2 import PdfFileReader

outfile = open("conversion.csv",'w',newline='')
writer = csv.writer(outfile)

infile = open('some.pdf', 'rb')
reader = PdfFileReader(infile)
contents = reader.getPage(15).extractText().split('\n')
for each in contents:
  writer.writerow(each.split('\n'))

infile.close()
outfile.close()

由于 vintol 非常接近我正在寻找的输出,我将接受他的解决方案作为答案。