TypeError: argument of type 'PSLiteral' is not iterable
TypeError: argument of type 'PSLiteral' is not iterable
我正在尝试使用 pdfform-scraper-script 删除一些隐藏的输入,然后再将其写入 csv 文件。但是我不断收到标题中提到的错误。相关的代码是:
import glob
import os
import sys
import csv
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdftypes import resolve1
path = 'C:\Users\Wonen\Downloads\Test'
for filename in glob.glob(os.path.join(path, '*.pdf')):
fp = open(filename, 'rb')
#read pdf's
parser = PDFParser(fp)
doc = PDFDocument(parser)
#doc.initialize() # <<if password is required
fields = resolve1(doc.catalog['AcroForm'])['Fields']
row = []
for i in fields:
field = resolve1(i)
name, value = field.get('T'), field.get('V')
#removing 'hidden enter'
if value == None:
print 'ok'
elif value == NotImplementedError:
print 'ok'
elif '\n' in value:
value.replace('\n',' ')
elif '\r' in value:
value.replace('\r',' ')
row.append(value)
writer.writerow(list(reversed(row)))
完整的错误(+输出)是:
还行
还行
Traceback (most recent call last): File
"C:\Python27\Scripts\test3.py", line 37, in
elif '\n' in value: TypeError: argument of type 'PSLiteral' is not iterable
有人知道怎么解决吗?
不知道输入文件的内容很难猜。我认为问题是你在调用 field.get('V')
时得到一些非字符串值来解决这个问题我建议你将 value
更改为字符串。
像这样尝试:
if value == None:
print 'ok'
elif value == NotImplementedError:
print 'ok'
elif '\n' in str(value):
value = str(value)
value.replace('\n',' ')
elif '\r' in str(value):
value = str(value)
value.replace('\r',' ')
我正在尝试使用 pdfform-scraper-script 删除一些隐藏的输入,然后再将其写入 csv 文件。但是我不断收到标题中提到的错误。相关的代码是:
import glob
import os
import sys
import csv
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdftypes import resolve1
path = 'C:\Users\Wonen\Downloads\Test'
for filename in glob.glob(os.path.join(path, '*.pdf')):
fp = open(filename, 'rb')
#read pdf's
parser = PDFParser(fp)
doc = PDFDocument(parser)
#doc.initialize() # <<if password is required
fields = resolve1(doc.catalog['AcroForm'])['Fields']
row = []
for i in fields:
field = resolve1(i)
name, value = field.get('T'), field.get('V')
#removing 'hidden enter'
if value == None:
print 'ok'
elif value == NotImplementedError:
print 'ok'
elif '\n' in value:
value.replace('\n',' ')
elif '\r' in value:
value.replace('\r',' ')
row.append(value)
writer.writerow(list(reversed(row)))
完整的错误(+输出)是:
还行
还行
Traceback (most recent call last): File "C:\Python27\Scripts\test3.py", line 37, in elif '\n' in value: TypeError: argument of type 'PSLiteral' is not iterable
有人知道怎么解决吗?
不知道输入文件的内容很难猜。我认为问题是你在调用 field.get('V')
时得到一些非字符串值来解决这个问题我建议你将 value
更改为字符串。
像这样尝试:
if value == None:
print 'ok'
elif value == NotImplementedError:
print 'ok'
elif '\n' in str(value):
value = str(value)
value.replace('\n',' ')
elif '\r' in str(value):
value = str(value)
value.replace('\r',' ')