我在那里尝试了将ansi转换为utf-8的答案.
import io
with io.open(file_path_ansi, encoding='latin-1', errors='ignore') as source:
with open(file_path_utf8, mode='w', encoding='utf-8') as target:
shutil.copyfileobj(source, target)
Run Code Online (Sandbox Code Playgroud)
但我得到"TypeError:'encoding'是这个函数的无效关键字参数"
我试过了
with io.open(file_path_ansi, encoding='cp1252', errors='ignore') as source:
Run Code Online (Sandbox Code Playgroud)
也是,并得到同样的错误.
然后我试了一下
import io
with io.open(file_path_ansi, encoding='latin-1', errors='ignore') as source:
with io.open(file_path_utf8, mode='w', encoding='utf-8') as target:
shutil.copyfileobj(source, target)
Run Code Online (Sandbox Code Playgroud)
仍然有同样的错误.我也试过cp1252,但得到了同样的错误.
我从几个stackoverflow问题中学到了这些
TypeError: 'encoding' is an invalid keyword argument for this function
Run Code Online (Sandbox Code Playgroud)
在python 2.x中经常出现错误消息
但主要是回答者建议以某种方式使用python 3.
在python 2.x中将ansi txt转换为utf-8 txt真的不可能吗?(我用2.7)
如何有效地居中对齐某些列而左对齐其他列?
我想做这样的事情。
-------------------------
| A |B |
-------------------------
| C |D |
-------------------------
Run Code Online (Sandbox Code Playgroud)
此代码使所有列居中。
{| class="wikitable" style="text-align: center;"
|-
! Header 1
! Header 2
|-
| A
| B
|-
| C
| D
|}
Run Code Online (Sandbox Code Playgroud)
而且我知道以下代码可以满足我的需求。
{| class="wikitable"
|-
! Header 1
! Header 2
|-
| style="text-align: center;" | A
| B
|-
| style="text-align: center;" | C
| D
|}
Run Code Online (Sandbox Code Playgroud)
但是因为我需要制作一个很长的桌子,所以这很乏味。
是否有任何代码可以使用非常简单的一行代码仅将第一列居中对齐?
当我试图编写一个将Ansi转换为UTF-8的python程序时,我发现了这一点
它将UTF-8转换为Ansi.
我认为通过扭转订单才会起作用.所以我编码了
file_path_ansi = "input.txt"
file_path_utf8 = "output.txt"
#open and encode the original content
file_source = open(file_path_ansi, mode='r', encoding='latin-1', errors='ignore')
file_content = file_source.read()
file_source.close
#write
file_target = open(file_path_utf8, mode='w', encoding='utf-8')
file_target.write(file_content)
file_target.close
Run Code Online (Sandbox Code Playgroud)
但它会导致错误.
TypeError: file<> takes at most 3 arguments <4 given>
Run Code Online (Sandbox Code Playgroud)
所以我改变了
file_source = open(file_path_ansi, mode='r', encoding='latin-1', errors='ignore')
Run Code Online (Sandbox Code Playgroud)
至
file_source = open(file_path_ansi, mode='r', encoding='latin-1')
Run Code Online (Sandbox Code Playgroud)
然后它会导致另一个错误:
TypeError: 'encoding' is an invalid keyword arguemtn for this function
Run Code Online (Sandbox Code Playgroud)
我该如何修复我的代码来解决这个问题?
我知道如何比较两个文件的代码。只需在notepad ++和Plugins-> Compare中打开两个代码
但是,当文件太多时,如何比较不同文件夹中的所有代码?
例如,假设在我的PC中有两个文件夹C:/ A和C:/ B
这两个文件夹具有相同的文件a.py和b.py
我想比较C:/ A中的a.py和C:/ B中的a.py,并比较C:/ A中的b.py和C:/ B中的b.py。
我有一个制表符分隔的txt文件.
我想替换
\t\t
Run Code Online (Sandbox Code Playgroud)
通过
\t999999999\t
Run Code Online (Sandbox Code Playgroud)
起初我尝试使用notepad ++来做到这一点.我做的.但它太慢了.
所以我想的是,如果我使用python,它是否会很快.
我做了一些搜索,发现正则表达式替换python中的文本
但似乎问题不在于替换像本身这样的东西,而只是用正则表达式来代替普通单词.
我的尝试没有用
import fileinput
for line in fileinput.FileInput("input.txt",inplace=1):
line = line.replace(r'\t\t',r'\t999999999\t')
print line,
Run Code Online (Sandbox Code Playgroud) python ×4
utf-8 ×2
css ×1
encoding ×1
html-table ×1
mediawiki ×1
notepad++ ×1
python-2.7 ×1
python-3.x ×1
regex ×1