我有一个文本文件,希望逐行转换为base64。使用熊猫1.0.1和python 3.7我有一个文本文件,其中包含由回车符分隔的单词,名为sampleTEXT.txt熊猫可以读取文件,并且其中包含数据
import pandas as pd
words = pd.read_table("sampleTEXT.txt",names=['word'],encoding="utf-8",header=None)
words.head()
--- word
---------------
---0 difference
---1 where
---2 mc
---3 is
---4 the
---
下一个jupyter单元格具有此代码:
BLOCKSIZE = 1048576 # or some other, desired size in bytes
with codecs.open(sampleTEXT.txt, "r", "utf-8") as sourceFile:
with codecs.open(bsampleTEXT.b64, "w", "base64") as targetFile:
while True:
contents = sourceFile.read(BLOCKSIZE)
if not contents:
break
targetFile.write(contents)
但我收到此错误:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-118-8aec7c80fc16> in <module>
1 import codecs
2 BLOCKSIZE = 1048576 # or some other, desired size in bytes
----> 3 with codecs.open(sampleTEXT.txt, "r", "utf-8") as sourceFile:
4 with codecs.open(bsampleTEXT.b64, "w", "base64") as targetFile:
5 while True:
NameError: name 'sampleACAD' is not defined
我知道文件在那里,这是可以的,因为熊猫可以将其作为数据框导入。我知道我使用的是here
中所述的正确语法我有这个要工作:
import io,codecs
BLOCKSIZE = 1048576 # or some other, desired size in bytes
with io.open("sampleACAD.txt",mode="rb") as sourceFile:
with codecs.open("bsampleACAD.b64", mode="w", encoding="base64") as targetFile:
while True:
contents = sourceFile.read(BLOCKSIZE)
if not contents:
break
targetFile.write(contents)
现在我只需要弄清楚如何将每个基数64个单词放在换行符上