Python 通过Pythion替换不同文件夹中阵列的某些部分 我的代码将替换整个文件。而我只需要一些替代品。 下面是我试图替换的文件。我只想更改“sheetname”中的一些单词:“Main” 我试图做的是替换“Main”“sheet”中的某些单词,即sheetname=Main; 提前谢谢。任何帮助都会很好! 下面是我的代码:

Python 通过Pythion替换不同文件夹中阵列的某些部分 我的代码将替换整个文件。而我只需要一些替代品。 下面是我试图替换的文件。我只想更改“sheetname”中的一些单词:“Main” 我试图做的是替换“Main”“sheet”中的某些单词,即sheetname=Main; 提前谢谢。任何帮助都会很好! 下面是我的代码:,python,arrays,json,python-3.x,python-2.7,Python,Arrays,Json,Python 3.x,Python 2.7,将文件中的数据命名为数据: data = [ { "sheetname": "Main", "emp_details": [ [ "スバム", "ksing.shubh@gmail.com", "marble", "intern" ], [ "Gaurav", "gaurav.sing

将文件中的数据命名为
数据

 data = [  
  {
   "sheetname": "Main",
   "emp_details": [
        [
            "スバム",
            "ksing.shubh@gmail.com",
            "marble",
            "intern"
        ],
        [
            "Gaurav",
            "gaurav.singh@cobol.in",
            "snacks",
            "デボロッパ"
        ],
        [
            "ニキル",
            "nikhil@geeksforgeeks.org",
            "tennis",
            "Full Time"
        ]
       ]
     },
     {
    "sheetname": "Next",
    "emp_details": [
            [
                "スバム",
                "ksing.shubh@gmail.com",
                "fabrics",
                "intern"
            ],
            [
                "Gaurav",
                "gaurav.singh@cobol.in",
                "xenom",
                "デボロッパ"
            ],
            [
                "ニキル",
                "nikhil@geeksforgeeks.org",
                "hiltop",
                "Full Time"
            ]
          ]
         }
        ,{
        "sheetname": "Last",
        "emp_details": [
                [
                    "スバム",
                    "ksing.shubh@gmail.com",
                    "trend",
                    "intern"
                ],
                [
                    "Gaurav",
                    "gaurav.singh@cobol.in",
                    "souvenir",
                    "デボロッパ"
                ],
                [
                    "ニキル",
                    "nikhil@geeksforgeeks.org",
                    "urban",
                    "Full Time"
                ]
            ]
        }
 ]

import re

word_to_replace = 'Gaurav'
word_to_assign = 'Singh'

for dictionary in data:
    if dictionary.get('sheetname') == 'Main':
        list_data = dictionary.get('emp_details')
        new_data = []
        for item in list_data:
            if word_to_replace in item:
                new_item = []
                for word in item:
                    if word.lower().find(word_to_replace.lower()) == 0:
                        new_word = re.sub(word_to_replace, word_to_assign, word)
                        if new_word == word:
                            new_word = re.sub(word_to_replace.lower(), word_to_assign.lower(), word)
                        new_item.append(new_word)
                    else:
                        new_item.append(word)
                new_data.append(new_item)
            else:
                new_data.append(item)

        dictionary['emp_details'] = new_data
输出:


这是一个json数据,如果您将其保存在一个文件中,则可以使用json模块加载该数据,并简单地进行迭代,然后添加您的条件

import json
import os
import sys

directory = os.listdir('/Users/Unicorn/Desktop/for all/change')
os.chdir('/Users/Unicorn/Desktop/for all/change')

for file in directory:
    open_file = open(file,'r', encoding='utf-8')
    read_file = open_file.read()
    open_file.close()
    # loads json file so it can be treated just like dictionary/list object in python
    data_json = json.loads(read_file)

    # iterate sheet item in data
    for sheet in data_json:
        # this is where you put your condition for sheetname == "Main"
        if sheet['sheetname'] == 'Main':
            # iterate over emp_detail because it is a list
            for emp_detail in sheet['emp_details']:
                # this is my simple method to replace item on a list
                if 'Gaurav' in emp_detail:
                    emp_detail[emp_detail.index('Gaurav')] = 'Singh'
    data_final = json.dumps(data_json)
    open_file = open(file,'w', encoding='utf-8')
    open_file.write(data_final)
    open_file.close()

您需要替换哪些数据?例如,对于“Main”到Singh的Gaurav。您的意思是“sheetname”中的“Gaurav”:“Main”到“Singh”?是的,它应该只在“sheetname”:“Main”中更改。嗨,仅使用粗体字体或标题有点像喊叫。使用
#
作为标题以更好地显示文章的结构是很好的,但是在所有文章中始终使用它们可能被视为不礼貌。不需要在这里修复它,但请在下一篇文章中克制自己。实际上,它正在工作,也不工作。我的意思是它在这种情况下工作得很好,但是日语字符变成了某种unicode。我还想得到输出文件的设计。但是非常感谢。我不知道为什么,但是代码不起作用。谢谢你的帮助。@Rubin你有什么错误吗?我已经在你列出的数据上测试过了<代码>数据需要一个字典列表。你能添加一个完整的代码吗,可能是因为我在什么地方弄错了。对不起打扰了@鲁宾将字典的赋值列表添加到变量
数据中
我这样做了。导入json word_to_replace='Gaurav'new_word='Singh'和open('new.json','r',encoding='utf-8')作为文件:data=json.load(file)用于data:if dictionary.get('sheetname')=='Main':…(复制了代码)。。。。。。。。。dictionary['emp_details']=打开('wow.json','w',encoding='utf-8')为文件的新_数据:json.dump(data,file,indent=4)问题是日语字符变成/u。。。像那样。
 data = [  
  {
   "sheetname": "Main",
   "emp_details": [
        [
            "スバム",
            "ksing.shubh@gmail.com",
            "marble",
            "intern"
        ],
        [
            "Gaurav",
            "gaurav.singh@cobol.in",
            "snacks",
            "デボロッパ"
        ],
        [
            "ニキル",
            "nikhil@geeksforgeeks.org",
            "tennis",
            "Full Time"
        ]
       ]
     },
     {
    "sheetname": "Next",
    "emp_details": [
            [
                "スバム",
                "ksing.shubh@gmail.com",
                "fabrics",
                "intern"
            ],
            [
                "Gaurav",
                "gaurav.singh@cobol.in",
                "xenom",
                "デボロッパ"
            ],
            [
                "ニキル",
                "nikhil@geeksforgeeks.org",
                "hiltop",
                "Full Time"
            ]
          ]
         }
        ,{
        "sheetname": "Last",
        "emp_details": [
                [
                    "スバム",
                    "ksing.shubh@gmail.com",
                    "trend",
                    "intern"
                ],
                [
                    "Gaurav",
                    "gaurav.singh@cobol.in",
                    "souvenir",
                    "デボロッパ"
                ],
                [
                    "ニキル",
                    "nikhil@geeksforgeeks.org",
                    "urban",
                    "Full Time"
                ]
            ]
        }
 ]

import re

word_to_replace = 'Gaurav'
word_to_assign = 'Singh'

for dictionary in data:
    if dictionary.get('sheetname') == 'Main':
        list_data = dictionary.get('emp_details')
        new_data = []
        for item in list_data:
            if word_to_replace in item:
                new_item = []
                for word in item:
                    if word.lower().find(word_to_replace.lower()) == 0:
                        new_word = re.sub(word_to_replace, word_to_assign, word)
                        if new_word == word:
                            new_word = re.sub(word_to_replace.lower(), word_to_assign.lower(), word)
                        new_item.append(new_word)
                    else:
                        new_item.append(word)
                new_data.append(new_item)
            else:
                new_data.append(item)

        dictionary['emp_details'] = new_data
[{'sheetname': 'Main', 'emp_details': [['スバム', 'ksing.shubh@gmail.com', 'marble', 'intern'], ['Singh', 'singh.singh@cobol.in', 'snacks', 'デボロッパ'], ['ニキル', 'nikhil@geeksforgeeks.org', 'tennis', 'Full Time']]}, {'sheetname': 'Next', 'emp_details': [['スバム', 'ksing.shubh@gmail.com', 'fabrics', 'intern'], ['Gaurav', 'gaurav.singh@cobol.in', 'xenom', 'デボロッパ'], ['ニキル', 'nikhil@geeksforgeeks.org', 'hiltop', 'Full Time']]}, {'sheetname': 'Last', 'emp_details': [['スバム', 'ksing.shubh@gmail.com', 'trend', 'intern'], ['Gaurav', 'gaurav.singh@cobol.in', 'souvenir', 'デボロッパ'], ['ニキル', 'nikhil@geeksforgeeks.org', 'urban', 'Full Time']]}]
import json
import os
import sys

directory = os.listdir('/Users/Unicorn/Desktop/for all/change')
os.chdir('/Users/Unicorn/Desktop/for all/change')

for file in directory:
    open_file = open(file,'r', encoding='utf-8')
    read_file = open_file.read()
    open_file.close()
    # loads json file so it can be treated just like dictionary/list object in python
    data_json = json.loads(read_file)

    # iterate sheet item in data
    for sheet in data_json:
        # this is where you put your condition for sheetname == "Main"
        if sheet['sheetname'] == 'Main':
            # iterate over emp_detail because it is a list
            for emp_detail in sheet['emp_details']:
                # this is my simple method to replace item on a list
                if 'Gaurav' in emp_detail:
                    emp_detail[emp_detail.index('Gaurav')] = 'Singh'
    data_final = json.dumps(data_json)
    open_file = open(file,'w', encoding='utf-8')
    open_file.write(data_final)
    open_file.close()