提取字典值到 .txt 文件中

huangapple go评论71阅读模式
英文:

Extracting dictionary values into .txt files

问题

from tqdm.auto import tqdm

text_data = []
file_count = 0

for sample in tqdm(new_dict):
    # Remove newline characters from each sample as we need to use them exclusively as separators
    sample_text = sample['text'].replace('\n', ' ')
    text_data.append(sample_text)
    if len(text_data) == 5_000:
        # Once we reach the 5K mark, save to a file
        with open(f'file_path/oscar_data/oscar_{file_count}.txt', 'w', encoding='utf-8') as fp:
            fp.write('\n'.join(text_data))
        text_data = []
        file_count += 1
英文:

I am looking to create .txt files from a dictionary, extracting text into new lines of each txt file - dictionary structure looks like:

{'id': 0,
 'text': 'Mtendere Village was inspired by the vision'}

I am using this code:

from tqdm.auto import tqdm  #loading bar

text_data = []
file_count = 0

for sample in tqdm(new_dict):
    # remove newline characters from each sample as we need to use exclusively as seperators
    sample = sample['text'].replace('\n', '\s')
    text_data.append(sample)
    if len(text_data) == 5_000:
        # once we hit the 5K mark, save to file
        with open('file_path\oscar_data\oscar_%s.txt' %file_count, 'w', encoding='utf-8') as fp:
            fp.write('\n'.join(text_data)) 
        text_data = []
        file_count += 1

However this gives me an error;

---> 12     sample = sample['text'].replace('\n', '\s') 
TypeError: 'int' object is not subscriptable

Although I understand what the error is telling me, I'm not sure how to correct it...

答案1

得分: 0

我认为您正在尝试将一个字典列表传递给循环,但实际上传递了一个字典。

from tqdm.auto import tqdm  #加载进度条

new_dict = [
	{
		'id': 0,
		'text': 'Mtendere Village was inspired by the vision'
	}
]

text_data = []
file_count = 0

for sample in tqdm(new_dict):
	# 从每个样本中移除换行符,因为我们需要将其专门用作分隔符
	sample = sample['text'].replace('\n', ' ')
	text_data.append(sample)
	if len(text_data) == 5000:
		# 一旦达到5K标记,将其保存到文件
		with open('file_path\\oscar_data\\oscar_%s.txt' %file_count, 'w', encoding='utf-8') as fp:
			fp.write('\n'.join(text_data)) 
		
		text_data = []
		file_count += 1

我已将new_dict更新为一个字典列表,并修复了这个问题。

英文:

I think you're trying to pass a list of dictionaries to the loop, but actually passed a dictionary.

from tqdm.auto import tqdm  #loading bar

new_dict = [
	{
		'id': 0,
		'text': 'Mtendere Village was inspired by the vision'
	}
]

text_data = []
file_count = 0

for sample in tqdm(new_dict):
	# remove newline characters from each sample as we need to use exclusively as seperators
	sample = sample['text'].replace('\n', '\s')
	text_data.append(sample)
	if len(text_data) == 5000:
		# Once we hit the 5K mark, save it to file
		with open('file_path\oscar_data\oscar_%s.txt' %file_count, 'w', encoding='utf-8') as fp:
			fp.write('\n'.join(text_data)) 
		
		text_data = []
		file_count += 1

I have updated new_dict to a list of dictionaries and it fixed the issue.

huangapple
  • 本文由 发表于 2023年7月6日 18:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定