【问题标题】:Replacing all single quotes outside of brackets to parse to valid json [closed]替换括号外的所有单引号以解析为有效的 json [关闭]
【发布时间】:2021-07-22 18:40:31
【问题描述】:

我有一个文件要解析为 json。第一项如下所示:

{'name': 'Ravikant P.', 'username': '@exp9993', 'url': 'https://www.freelancer.com/u/exp9993', 'title': 'ASP.NET/ Graphic Design/ Web Design/WordPress/PHP', 'city': 'Indore, India', 'stars_num_reviews': ['@exp9993', '5.0', '(178 reviews)'], 'rate': '$15 USD / hour', 'reputation': ['99%', 'Jobs Completed', '87%', 'On Budget', '94%', 'On Time', '19%', 'Repeat Hire Rate'], 'review_ratings': ['5.0', '5.0', '5.0', '5.0', '5.0'], 'review_clean_project_values': ['€50.00 EUR', '$600.00 USD', '$1,300.00 AUD', '$30.00 USD', '$50.00 USD'], 'review_project_titles': ['Wordpress - Woocommerce Small Function', 'PayDash Frontend', 'Improve current website', 'Project for Ravikant P.', 'Build a page 1:1 copy from Figma design file'], "review_project_descriptions": ['Wordpress - Woocommerce Small Function', 'Very fast and efficient! Made my request in less than 2-3 hours and exactly what i asked for.\nI recommend and will come back for sure! :)', 'PayDash Frontend', 'Delivered a high value project very quickly, always helping me fix any bugs there are in the code. Very great developer and team to work with!', 'Improve current website', 'Amazing job, and nothing was too much trouble. Highly recommend!!', 'Project for Ravikant P.', 'I requested the project from him, he completed it within 30 minutes. Super quick delivery, I will definitely be using him in the future for all of my frontend web development projects.', 'Build a page 1:1 copy from Figma design file', "He did the work very quickly and was super precise about all the little details. He's the perfect person I was looking for, someone who was able to create a page for me while paying attention to all… Read more"], 'experience_title': 'N/A', 'education_title': 'bachelor of engineering', 'education_time': '2011 - 2015', 'description': 'We can make anything you want, if you can describe exactly what you want, will get the exact service until full satisfaction.    My interest lies in designing new material & do believe we are creative & can handle complications, I am always eager to learn new things & by listening carefully & asking the right questions can get to the core of the conversation quickly.        We are a Team of 9+ experienced professionals who work closely with the clients, understand their requirements, offer suggestions, and implement ideas into reality. We always think beyond the boundaries and provide user-friendly as well as high quality IT services to our customers at a very reasonable price. Our team is always dedicative to innovate from high-end E-Commerce website development to the simplest logo design needs… Read more', 'profile_skills': {'Website Design': '138', 'HTML': '129', 'Graphic Design': '115', 'PHP': '99', 'WordPress': '62', 'User Experience Design': '53', 'CSS': '26', 'User Interface / IA': '25', 'Photoshop': '17', 'C# Programming': '13', 'ASP.NET': '12', '.NET': '11', 'eCommerce': '9', 'Microsoft SQL Server': '8', 'Logo Design': '8', 'PSD to HTML': '6', 'WooCommerce': '6'}}

为了成为有效的json,所有的单引号都应该转换成双引号。但是,替换任何 [] 中的单引号会截断 json。我已经进行了一些广泛的搜索,但还没有看到使用正则表达式匹配引号中不包含的文本然后更改该文本的方法。关于如何将其解析为 json 的任何想法?我在下面包含了第 2 项和第 3 项。任何帮助将不胜感激!我已经坚持了几天了,我不确定还有什么可以尝试的......

{'name': 'Artur', 'username': '@Appswebandroid', 'url': 'https://www.freelancer.com/u/Appswebandroid', 'title': '♛Google Certified Digital Marketing - Grow Sales♛', 'city': 'Mafra, Portugal', 'stars_num_reviews': ['@Appswebandroid', '5.0', '(71 reviews)'], 'rate': '$15 USD / hour', 'reputation': ['100%', 'Jobs Completed', '94%', 'On Budget', '97%', 'On Time', '17%', 'Repeat Hire Rate'], 'review_ratings': ['5.0', '5.0', '5.0', '5.0', '5.0'], 'review_clean_project_values': ['$10.00 USD', '$10.00 USD', '$200.00 USD', '$100.00 USD', '$196.00 USD'], 'review_project_titles': ['woodpress seo expert', 'semana adwords', 'Project for Artur', 'experto en adwords de preferencia en español', 'Google ADS expert for make a campaign today!'], "review_project_descriptions'": ['woodpress seo expert', 'Thank you', 'semana adwords', "Artur helped me setup my campaign, but he was also taking care of it during the month that we were working. I couldn't have done it myself. He helped me channel all my money to the right… Read more", 'Project for Artur', 'Great Freelancer, very pro- and reactive. Only recommend. Thank you, Artur!', 'experto en adwords de preferencia en español', 'Artur is a master of adwords and he is really helpfull. Would work with him again. Thanks :)', 'Google ADS expert for make a campaign today!', 'Good work, is an expert in google ads! Fully recommend!'], 'experience_title': 'Computer Technician - Network Installation and Management', 'experience_time': 'Feb 2018 - Jun 2020 (2 years, 4 months)', 'education_title': 'Work optimization Business', 'education_time': 'N/A', 'description': 'I have a degree in Computer Technician Installation and Network Management.  I am available to help you publicize your business to get more customers and more sales and solve web problems. I am ready to exceed my limits to satisfy every customer.  Google Certified Digital Marketing Expert  Increase your organic traffic and the average position of your keywords with SEO method.  Qualified traffic in Google Adwords, Facebook, Instagram, Linkedin campaigns.  Increase your sales for your company, in ecommerce or physical companies  We place your site on the first page of search engines.  I am a multi-site webmaster, an SEO specialist with a passion for search engines, taking your site or business to the top of Google search, following best practices.    ✅My skills:  *✔️ Google Merchant Center*✔️Facebook Ads*✔️Google Ads*✔️Instagram Ads✔️Pinterest Ads*✔️SEO*✔️WordPress*✔️Website Optimization*✔️eCommerce*✔️ Facebook Marketing**✔️Social Media… Read more', 'profile_skills': {'Internet Marketing': '35', 'Google Adwords': '27', 'Facebook Marketing': '23', 'Marketing': '22', 'Advertising': '20', 'SEO': '14', 'Social Media Marketing': '12', 'Android': '10', 'PHP': '8', 'Mobile App Development': '7', 'Google Adsense': '6', 'Website Design': '6', 'Linux': '5', 'Graphic Design': '5', 'eCommerce': '5', 'Google Analytics': '4', 'Prestashop': '4'}}
{'name': 'Usman N.', 'username': '@futivetechnet', 'url': 'https://www.freelancer.com/u/futivetechnet', 'title': '3D/2D Design/Animation-SMM-Unity Game Development', 'city': 'Lahore, Pakistan', 'stars_num_reviews': ['@futivetechnet', '4.9', '(139 reviews)'], 'rate': '$25 USD / hour', 'reputation': ['96%', 'Jobs Completed', '96%', 'On Budget', '93%', 'On Time', '12%', 'Repeat Hire Rate'], 'review_ratings': ['5.0', '5.0', '5.0', '5.0', '5.0'], 'review_clean_project_values': ['$12,400.00 USD', '€200.00 EUR', '$150.00 USD', '€4,700.00 EUR', '•'], 'review_project_titles': ['Online Easter crepes hunt game (for kids 5-10)', 'Project for Usman N.', '12 Social media posts for Digital marketing and app development company', 'Mobile game similar to Flappy Bird (side-scroller)', 'Make character animation for 2D game'], "review_project_descriptions'": ['Online Easter crepes hunt game (for kids 5-10)', 'It was a pleasure working with Usman. They made an amazing job!', 'Project for Usman N.', "Great quality of work! It's my second project with Usman, and both were successful. Looking forward to hire him again.", '12 Social media posts for Digital marketing and app development company', 'Usman delivered the posts as per the requirements and he did multiple revisions as per our requirements', 'Mobile game similar to Flappy Bird (side-scroller)', 'Great communication, great project management and great work!', 'Make character animation for 2D game', 'Great work! Usman understands all requirements with minimum clarifications. Result is very good.'], 'experience_title': '3D/2D Design/Animation-SMM-Unity Game Development', 'experience_time': 'Mar 2010 - Present', 'education_title': 'N/A', 'education_time': 'N/A', 'description': 'We are an all in one IT services provider in this region. A team of 70+ skilled & certified professional developers, designers, video editors, 3D modellers & animators, project managers and quality assurance individuals.      Our team is dedicated in providing the work to our clients, meeting the highest quality standards, client specifications & timely delivery of services.      Mobile | PC Games Development:   - Experienced in Unity3D, iOS Swift, Cocos2D, Buildbox, including team for UI/UX designs   - Experienced in Action, Simulation, 2D platformers, multiplayer & arcade games      3D | 2D Works, CGI Graphics:   - 3D Modelling, Rigging, Rendering & Animation   - Expertise in Autodesk Maya, 3DS Max, Blender Studio, DAZ Studio, Adobe Flash & After Effects… Read more', 'profile_skills': {'Animation': '64', '3D Animation': '64', 'Mobile App Development': '53', 'Game Development': '52', 'Graphic Design': '51', 'Game Design': '47', 'Unity 3D': '26', 'Video Services': '26', 'After Effects': '26', '3D Modelling': '24', 'Android': '20', '3D Rendering': '20', 'Photoshop': '18', '3D Design': '15', 'PHP': '14', 'Video Editing': '13', 'Website Design': '13'}}

【问题讨论】:

  • 首先将其转储为 JSON。这是由 Python 解释器创建的吗?
  • 如果它是 Python 数据结构的 repr()esentation,您可以使用 ast.literal_eval() 安全地解析它,然后使用 json.dumps() 转储它。不要自己尝试在格式之间进行混搭,那样会很疯狂(通过repr() 选择以不同方式序列化的字符串,因此不遵循与示例数据相同的规则)
  • 你说的很对@Charles。我打算以错误的方式解析它。非常感谢你们的帮助!我能够将其放入数据框中。

标签: python json regex


【解决方案1】:

正如@CharlesDuffy 所说,您可以使用ast.literal_eval()

您可以直接从文件中读取内容:

# file.txt contains three lines, one for each of the OP's strings
with open('file.txt') as f:
    dlist = [ast.literal_eval(s) for s in f.read().splitlines()]
>>> [len(d) for d in dlist]
[17, 18, 18]

请注意,在您提供的字符串中,有一个 r'\n':

"""...and exactly what i asked for.\nI recommend..."""

因此,如果您尝试将""" 包围的字符串粘贴到解释器中,那么这会使ast.literal_eval() 阻塞输入(因为突然在字符串中间有一个换行符)。

如果你替换它,那么一切都很好。

dct = ast.literal_eval(s.replace('\n', r'\n'))
>>> len(dct)
17

但是当你从文件中读取时这是不必要的。

【讨论】:

  • 好吧,我想我只是以错误的方式处理它。在错误的地方寻找答案。这就像一个魅力!非常感谢皮埃尔!!!
猜你喜欢
  • 1970-01-01
  • 2020-12-28
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 2018-11-18
  • 2017-03-12
  • 1970-01-01
相关资源
最近更新 更多