擅长:python、mysql、java
<p>这将检查没有换行符的数据:</p>
<pre><code>f=open('abc.txt','r')
dodgy=False
if '\n' not in f.read(1024):
print "Dodgy file - No linefeeds in the first Kb"
dodgy=True
f.seek(0)
if dodgy==False: #read the first 100 lines
for x in range(1,101):
try: line = next(f)
except Exception as e: break
print('{0}: {1}'.format(x, line))
else: #read the first n bytes
line = f.read(1024)
print('bytes: '+line)
f.close()
</code></pre>