Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

pandas - Remove carriage return and newline feeds within a list of dictionaries - Python

I have a JSON file which I am reading into my Python script, flattening and then exporting it as a CSV.

My problem is that I noticed there are various carriage returns and newline feeds within the JSON file so it's messing up the whole structure of the CSV.

Updated Current Code:

from pymongo import MongoClient
import pandas as pd
from azure.storage.filedatalake import DataLakeServiceClient
from azure.core._match_conditions import MatchConditions
from azure.storage.filedatalake._models import ContentSettings
from pandas import json_normalize
from datetime import datetime, timedelta
import numpy as np

mongo_client = MongoClient("XXXX") 
db = mongo_client.scaling
table = db.planning
document = table.find()
docs = list(document)
docs = json_normalize(docs) 
docs['pressure'] = docs['pressure'].str.strip().str.replace(" 
","")
docs.to_csv("planning.csv", sep = ",",index=False) 

I'm getting the following error:

Traceback (most recent call last):
  File "XXXXV2.py", line 16, in <module>
    docs['pressureLevels'] = docs['pressureLevels'].str.strip().str.replace(" 
","")
  File "XXXX.venvlibsite-packagespandascoregeneric.py", line 5456, in __getattr__
    return object.__getattribute__(self, name)
  File "XXXX.venvlibsite-packagespandascoreaccessor.py", line 180, in __get__
    accessor_obj = self._accessor(obj)
  File "XXXX.venvlibsite-packagespandascorestringsaccessor.py", line 154, in __init__
    self._inferred_dtype = self._validate(data)
  File "XXXX.venvlibsite-packagespandascorestringsaccessor.py", line 218, in _validate
    raise AttributeError("Can only use .str accessor with string values!")
AttributeError: Can only use .str accessor with string values!

How do I get rid of the carriage returns, newline feeds when there's an integer present in the dictionary?

Any help will be appreciated.

question from:https://stackoverflow.com/questions/65846852/remove-carriage-return-and-newline-feeds-within-a-list-of-dictionaries-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are getting the error since you are trying to use strip with int object.

Try this:

for i in docs:
    x = {}
    for k, v in i.items():
        if type(v) == str:
            x[k.strip()] = v.strip().replace("
","")
        else:
            x[k.strip()] = v
    docs2.append(x)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...