文本fi中字母的置乱

2024-03-29 08:45:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我准备了一个测试数据,必须有不同的字母,比如说அ-20次 ம-30倍,த-40倍….(它们是UTF-8编码支持的泰米尔语字母) 这可以通过使用print语句来实现

{print ( ' ம் ' * 30 ) + ( ' த ' * 40  ) + }

但是,我需要把它们打乱,这样它们就不会以任何特定的顺序出现。我有大约230多封信,我会打印20,30,40次。然后我需要对它们进行置乱并将它们写入输出文件。 这方面的任何帮助都是有帮助的。在


Tags: 文件编码顺序字母语句utfprint测试数据
3条回答

归功于我的朋友@AswinMurugesh,他帮助我编写了代码。在

下面的代码做到了这一点。在

import codecs
import tamil
from random import shuffle

inp_file = codecs.open("/home/sibi/Desktop/scramble.txt",encoding="utf-8")
inp_text = inp_file.read().rstrip()

tamil_letters = tamil.utf8.get_letters(inp_text)
shuffle(tamil_letters)
tamil_letters = "".join(tamil_letters).encode("utf-8")
print tamil_letters

out_file = open('outputscrambled.txt','w')
out_file.write(tamil_letters)

我建议把这个问题分成三个部分:把你的字母表整理一下,然后把它写进一个文件里。请注意,下面代码中的第一行应该位于python文件的顶部,以允许您在源代码中使用utf-8字符。在

# -*- coding: utf-8 -*-

import codecs  # To write UTF-8 characters to a file
import random

# Assemble data list    
letters = [u'அ', u'ம', u'த']
data = []  # This list will hold the shuffled data

for current_letter in letters:
    # Choose how many times to repeat the current letter.
    times_repeated = random.choice([20, 30, 40])
    data.extend([current_letter] * times_repeated)

# Now, shuffle the 'data' list
random.shuffle(data)

# Now write the shuffled list to a file as one continuous string
data_string = "".join(data)

with codecs.open("data.txt", "w", "utf-8") as f:
    f.write(data_string)

请注意,如果您知道希望每个字母出现多少次,您可以将这些信息放入字典中,而不是从[20, 30, 40]中随机选择:

^{pr2}$

你可以用很多方法来解决这个问题。最有效的方法是使用^{} module。在

^{}

>>> from random import shuffle
>>> my_string = list('This is a test string.')
>>> shuffle(my_string)
>>> scrambled = ''.join(my_string)
>>> print(scrambled)
.sTtha te s rtisns gii

为此,您必须从字符串的字符中创建一个list,因为字符串是immutable。在

^{bq}$

^{}

^{pr2}$

您不必为此创建list;因为根据random.sample文档:

Returns a new list containing elements from the population while leaving the original population unchanged.

The ^{} built-in带{a6}

>>> from random import random
>>> my_string = 'This is a test string.'
>>> scrambled = sorted(my_string, key=lambda i: random())
>>> scrambled = ''.join(scrambled)
>>> print(scrambled)
ngi rts ithsT.staie s 

你也不需要这个。来自sorted文档:

Return a new sorted list from the items in iterable.

因为在Python中,字符串被视为iterable(见下文),因此可以对其使用sorted。在

iterable定义为

An object capable of returning its members one at a time.

相关问题 更多 >