相关疑难解决方法(0)

在UNIX中标识和删除空字符

我有一个包含不需要的空字符的文本文件(ASCII NUL,\0).当我尝试查看它时,vi我看到^@符号,在普通文本中交错.我怎么能够:

  1. 确定文件中的哪些行包含空字符?我曾尝试grepping为\0\x0,但没有奏效.

  2. 删除空字符?strings在文件上运行清理它,但我只是想知道这是否是最好的方法?

unix shell null special-characters

84
推荐指数
5
解决办法
11万
查看次数

如何在Python中将文件转换为utf-8?

我需要在Python中将一堆文件转换为utf-8,而我在"转换文件"部分时遇到了麻烦.

我想做相当于:

iconv -t utf-8 $file > converted/$file # this is shell code
Run Code Online (Sandbox Code Playgroud)

谢谢!

python encoding file utf-8

54
推荐指数
6
解决办法
9万
查看次数

python 3.1.3是否支持csv模块中的unicode?

我一直在使用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阅读.它可以为我节省很多工作.

python csv

8
推荐指数
1
解决办法
6625
查看次数

通过继承`io.TextIOWrapper`对子文件进行子类化 - 但它的构造函数有哪些签名?

我试图子类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.

python io subclassing python-3.x

8
推荐指数
1
解决办法
606
查看次数

Pythonic方法比较两个CSV文件以跟踪更改

我有一个生成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 csv comparison

0
推荐指数
1
解决办法
2175
查看次数