本文整理汇总了Python中pyaid.string.StringUtils.StringUtils.isTextType方法的典型用法代码示例。如果您正苦于以下问题:Python StringUtils.isTextType方法的具体用法?Python StringUtils.isTextType怎么用?Python StringUtils.isTextType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyaid.string.StringUtils.StringUtils
的用法示例。
在下文中一共展示了StringUtils.isTextType方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save
# 需要导入模块: from pyaid.string.StringUtils import StringUtils [as 别名]
# 或者: from pyaid.string.StringUtils.StringUtils import isTextType [as 别名]
def save(self, path =None):
""" Saves the CSV file data to the specified path """
if path is None:
path = self.path
if self.removeIfSavedEmpty and not self.rows:
self.remove()
return
index = 0
names = self.fieldNames
if self.autoIndexFieldName:
names.insert(0, self.autoIndexFieldName)
try:
with open(path, 'wb') as f:
writer = csv.DictWriter(f, fieldnames=names, dialect=csv.excel)
writer.writeheader()
for row in self.rows:
result = dict()
if self.autoIndexFieldName:
index += 1
result[self.autoIndexFieldName] = index
for key, spec in DictUtils.iter(self._fields):
value = row.get(key, spec.get('empty', ''))
name = spec.get('name', key)
if StringUtils.isTextType(value):
value = value.encode('latin-1')
result[name] = value
writer.writerow(result)
return True
except Exception:
return False
示例2: save
# 需要导入模块: from pyaid.string.StringUtils import StringUtils [as 别名]
# 或者: from pyaid.string.StringUtils.StringUtils import isTextType [as 别名]
def save(self):
"""save doc..."""
if self.removeIfSavedEmpty and not self.rows:
self.remove()
return
index = 0
names = self.fieldNames
if self.autoIndexFieldName:
names.insert(0, self.autoIndexFieldName)
fieldNames = []
for name in names:
if StringUtils.isTextType(name):
name = name.encode('latin-1')
fieldNames.append(name)
try:
if sys.version < '3':
args = dict(mode='w')
else:
args = dict(mode='w', encoding='utf-8')
with open(self.path, **args) as f:
writer = csv.DictWriter(f, fieldnames=names, dialect=csv.excel)
writer.writeheader()
for row in self.rows:
result = dict()
if self.autoIndexFieldName:
index += 1
result[self.autoIndexFieldName] = index
for key, name in self._fields.items():
value = row.get(key, '')
if StringUtils.isBinaryType(value):
value = StringUtils.toText(value)
result[name] = value
writer.writerow(result)
return True
except Exception as err:
raise
return False
示例3: read
# 需要导入模块: from pyaid.string.StringUtils import StringUtils [as 别名]
# 或者: from pyaid.string.StringUtils.StringUtils import isTextType [as 别名]
def read(self, session, analysisSession, path =None, compressed =False):
""" Reads from the spreadsheet located at the absolute path argument and adds each row
to the tracks in the database. """
if path is not None:
self._path = path
if self._path is None:
return False
model = Tracks_Track.MASTER
for existingTrack in session.query(model).all():
self.remainingTracks[existingTrack.uid] = existingTrack.fingerprint
try:
data = pd.read_csv(self._path)
except Exception as err:
self._writeError({
'message':'ERROR: Unable to read CSV file "%s"' % self._path,
'error':err })
return
if data is None:
self._writeError({
'message':'ERROR: Failed to create CSV reader for file "%s"' % self._path })
return
for index, row in data.iterrows():
# Skip any rows that don't start with the proper numeric index value, which
# includes the header row (if it exists) with the column names
try:
index = int(row[0])
except Exception:
continue
rowDict = dict()
for column in Reflection.getReflectionList(TrackCsvColumnEnum):
value = row[column.index]
if value and StringUtils.isStringType(value) and not StringUtils.isTextType(value):
# Try to decode the value into a unicode string using common codecs
for codec in ['utf8', 'MacRoman', 'utf16']:
try:
decodedValue = value.decode(codec)
if decodedValue:
value = decodedValue
break
except Exception:
continue
try:
# Check to see if the value is NaN, and if it is replace it with an empty
# string to be ignored during import
value = '' if np.isnan(value) else value
except Exception:
pass
if value != '' or value is None:
rowDict[column.name] = value
self.fromSpreadsheetEntry(rowDict, session)
for uid, fingerprint in DictUtils.iter(self.remainingTracks):
# Iterate through the list of remaining tracks, which are tracks not found by the
# importer. If the track is marked as custom (meaning it is not managed by the importer)
# it is ignored. Otherwise, the track is deleted from the database as a track that no
# longer exists.
track = Tracks_Track.MASTER.getByUid(uid, session)
if track.custom:
continue
Tracks_Track.removeTrack(track, analysisSession)
self._logger.write('[REMOVED]: No longer exists "%s" (%s)' % (
track.fingerprint, track.uid))
session.flush()
for track in self.created:
self._logger.write('[CREATED]: "%s" (%s)' % (track.fingerprint, track.uid))
return True