我一直在尝试为上传到我的环境中的txt文件构建一个验证规则。文件以制表符分隔,我需要验证前3行的格式,例如:
## This Text Here
## This Text Here
## This Text Here
我需要建立一个通过-失败验证。到目前为止,我已经尝试过在python中使用内置的csv函数来实现这一点,但没有成功。如果能就最佳路线提供任何建议,我将不胜感激。
试试这个:
### it depends on how you open the file but...
# open using with..
with open("test.tsv") as inData:
# split lines on tabs...
allLines = [l.split("t") for l in inData]
# get the lines in question:
testLines = [l[0] for l in allLines[:3]]
# then you could use assert
for l in testLines:
assert(l.startswith("##"))
# and whatever other validation you need for the string
### you could ad try/except
try:
for l in testLines:
assert(l.startswith("##"))
except AssertionError as e:
print(e, "please use a validated file!")
进一步阅读:https://www.tutorialspoint.com/python/python_exceptions.htm
也许你应该试试熊猫:
import pandas as pd
file_name = # your file name
csv = pd.read_csv(file_name, sep='t')
# do your stuff
文件:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html