如何将 LF 转换为 CRLF? [英] How do I convert LF to CRLF?

查看:123
本文介绍了如何将 LF 转换为 CRLF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上找到了大部分英文单词的列表,但换行符是 unix 风格的(以 Unicode 编码:UTF-8).我在这个网站上找到了它:http://dreamsteep.com/projects/the-english-open-word-list.html

I found a list of the majority of English words online, but the line breaks are of unix-style (encoded in Unicode: UTF-8). I found it on this website: http://dreamsteep.com/projects/the-english-open-word-list.html

如何将换行符转换为 CRLF,以便我可以遍历它们?我将在其中使用它们的程序遍历文件中的每一行,因此每行必须有一个单词.

How do I convert the line breaks to CRLF so I can iterate over them? The program I will be using them in goes through each line in the file, so the words have to be one per line.

这是文件的一部分:bitbackbitebackbiterbackbitersbackbitesbackbitingbackbittenbackboard

应该是:

bit
backbite
backbiter
backbiters
backbites
backbiting
backbitten
backboard

如何将我的文件转换为这种类型?注意:它是 26 个文件(每个字母一个),总共 80,000 字左右(所以程序应该非常快).

How can I convert my files to this type? Note: it's 26 files (one per letter) with 80,000 words or so in total (so the program should be very fast).

我不知道从哪里开始,因为我从未使用过 unicode.提前致谢!

I don't know where to start because I've never worked with unicode. Thanks in advance!

使用 rU 作为参数(按照建议),在我的代码中使用:

Using rU as the parameter (as suggested), with this in my code:

with open(my_file_name, 'rU') as my_file:
    for line in my_file:
        new_words.append(str(line))
my_file.close()

我收到此错误:

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    addWords('B Words')
  File "D:\my_stuff\Google Drive\documents\SCHOOL\Programming\Python\Programming Class\hangman.py", line 138, in addWords
    for line in my_file:
  File "C:\Python3.3\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 7488: character maps to <undefined>

谁能帮我解决这个问题?

Can anyone help me with this?

推荐答案

您应该能够使用 Python 的 通用换行支持:

Instead of converting, you should be able to just open the file using Python's universal newline support:

f = open('words.txt', 'rU')

(注意U.)

这篇关于如何将 LF 转换为 CRLF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆