我有一个包含不需要的空字符的文本文件(ASCII NUL,\0).当我尝试查看它时,vi我看到^@符号,在普通文本中交错.我怎么能够:
确定文件中的哪些行包含空字符?我曾尝试grepping为\0和\x0,但没有奏效.
删除空字符?strings在文件上运行清理它,但我只是想知道这是否是最好的方法?
我需要在Python中将一堆文件转换为utf-8,而我在"转换文件"部分时遇到了麻烦.
我想做相当于:
iconv -t utf-8 $file > converted/$file # this is shell code
Run Code Online (Sandbox Code Playgroud)
谢谢!
我一直在使用python 2.6.我正在编写一个python程序来处理来自sql server的查询结果(以csv格式).我发现它不支持unicode.
当我用csv文件运行程序时,出现错误说:
for row in csvReader:
Error: line contains NULL byte
Run Code Online (Sandbox Code Playgroud)
用Ultraedit以ANSI/ASCII格式保存csv文件后,程序运行正常.
我试图包含编码选项,但它失败了:
csvReader = csv.reader(open(fname, mode='rb', encoding='unicode'), delimiter=',')
TypeError: 'encoding' is an invalid keyword argument for this function
csvReader = csv.reader(open(fname, mode='rb', encoding='utf-8'), delimiter=',')
TypeError: 'encoding' is an invalid keyword argument for this function
Run Code Online (Sandbox Code Playgroud)
我想知道python 3是否支持这个unicode阅读.它可以为我节省很多工作.
我试图子类io.TextIOWrapper下面这篇文章,虽然我的目标是不同的.从这开始(NB:动机):
class MyTextIOFile(io.TextIOWrapper):
def read(self, *args):
cont = super().read(*args)
return cont.replace("\x00", "")
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用我的构造函数打开文件
In [81]: f = MyTextIOFile("file.csv")
Run Code Online (Sandbox Code Playgroud)
但这给了:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-90-343e18b2e32f> in <module>()
----> 1 f = MyTextIOFile("file.csv")
AttributeError: 'str' object has no attribute 'readable'
Run Code Online (Sandbox Code Playgroud)
事实上,似乎io.TextIOWrapper构造函数希望传递一个文件对象.通过反复试验,我发现这个文件对象需要以二进制模式打开.但是我无法在任何地方找到文档,而且我不想构建在无证件行为之上(实际上,尝试继续使用它已经导致我在尝试传递对象时遇到问题csv.reader).在Python 3中对文件对象进行子类化的正确和受支持的方法是什么?
我正在使用Python 3.5.0.
我有一个生成CSV的Python脚本(从网站解析的数据).以下是CSV文件的示例:
File1.csv
China;Beijing;Auralog Software Development (Deijing) Co. Ltd.;;;
United Kingdom;Oxford;Azad University (Ir) In Oxford Ltd;;;
Italy;Bari;Bari, The British School;;Yes;
China;Beijing;Beijing Foreign Enterprise Service Group Co Ltd;;;
China;Beijing;Beijing Ying Biao Human Resources Development Limited;;Yes;
China;Beijing;BeiwaiOnline BFSU;;;
Italy;Curno;Bergamo, Anderson House;;Yes;
Run Code Online (Sandbox Code Playgroud)
File2.csv
China;Beijing;Auralog Software Development (Deijing) Co. Ltd.;;;
United Kingdom;Oxford;Azad University (Ir) In Oxford Ltd;;;
Italy;Bari;Bari, The British School;;Yes;
China;Beijing;Beijing Foreign Enterprise Service Group Co Ltd;;;
China;Beijing;Beijing Ying Biao Human Resources Development Limited;;Yes;
This;Is;A;New;Line;;
Italy;Curno;Bergamo, Anderson House;;Yes;
Run Code Online (Sandbox Code Playgroud)
如你看到的,
中国;北京;北外在线BFSU ;;; ==>来自File1.csv的这一行在File2.csv和This; Is; …
python ×4
csv ×2
comparison ×1
encoding ×1
file ×1
io ×1
null ×1
python-3.x ×1
shell ×1
subclassing ×1
unix ×1
utf-8 ×1