Stripping and renaming filesHow do I check whether a file exists without exceptions?How do I copy a file in Python?How do I list all files of a directory?How to read a file line-by-line into a list?Delete a file or folderHow to read a text file into a string variable and strip newlines?Renaming columns in pandasHow do I write JSON data to a file?How to prettyprint a JSON file?Python logging typeerror

A faster way to compute the largest prime factor

Double-nominative constructions and “von”

All ASCII characters with a given bit count

Bayes factor vs P value

What does "function" actually mean in music?

Older movie/show about humans on derelict alien warship which refuels by passing through a star

A Paper Record is What I Hamper

Restricting the options of a lookup field, based on the value of another lookup field?

How do I reattach a shelf to the wall when it ripped out of the wall?

Is Diceware more secure than a long passphrase?

std::unique_ptr of base class holding reference of derived class does not show warning in gcc compiler while naked pointer shows it. Why?

Can a stored procedure reference the database in which it is stored?

Is there metaphorical meaning of "aus der Haft entlassen"?

Are there moral objections to a life motivated purely by money? How to sway a person from this lifestyle?

Scheduling based problem

How much cash can I safely carry into the USA and avoid civil forfeiture?

Do I need to watch Ant-Man and the Wasp and Captain Marvel before watching Avengers: Endgame?

How long after the last departure shall the airport stay open for an emergency return?

What is this word supposed to be?

Work requires me to come in early to start computer but wont let me clock in to get paid for it

Contradiction proof for inequality of P and NP?

How do I produce this symbol: Ϟ in pdfLaTeX?

How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?

Help with my training data



Stripping and renaming files


How do I check whether a file exists without exceptions?How do I copy a file in Python?How do I list all files of a directory?How to read a file line-by-line into a list?Delete a file or folderHow to read a text file into a string variable and strip newlines?Renaming columns in pandasHow do I write JSON data to a file?How to prettyprint a JSON file?Python logging typeerror






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have written some Python modifying existing code to run through some JSON files and strip out some text. The end result should be saving the now edited file with the same file name "- clean.json" at the end.



I already have the code that loops through the input path, and all JSON files complete. I have also completed and tested that each file does open and the desired text is removed.



The final area is where I need help, how do I now take that updated file and save it back with an updated name. That updated name being -clean.json



All help is appreciated, script is included below:



def normalize_file(file):
global line_number
for line in file:
line_number += 1
json_object = json.loads(line)
logging.info("hi")
if not isinstance(json_object, dict):
logging.error('%s: Top level record must be a dict but was a %s',
line_number, type(json_object))
continue
try:
normalized_dict = normalize_dict(json_object)
except Exception as e:
logging.error('%s: %s', line_number, e)
continue
json.dump(normalized_dict, sys.stdout)
print()
logging.info("Processed %s lines", line_number)

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
logging.info("hi")
input_path = input("Enter path to JSON files->")
json_folder_path = os.path.join(input_path)
json_files = [pos_json for pos_json in os.listdir(input_path) if pos_json.endswith('.json')]
print(json_files)
for index, js in enumerate(json_files):
with open(os.path.join(input_path,js)) as json_file:
normalize_file(json_file)









share|improve this question
























  • Please reduce your code to a minimum, highlighting where exactly you need help. As it is, your code has several issues - it is unclear which one you need help with. For example, you ignore the user input, defying 1. You never open any files, so how do you know that 3 is working? You are loading several JSON from each file in a fragile manner, but your description implies otherwise.

    – MisterMiyagi
    Mar 22 at 16:40







  • 1





    @MisterMiyagi - understood, i have posted my updated code. the main body and function it calls only. I have also simplified the question I believe. Thanks for your feedback.

    – SSISPissesMeOff
    Mar 22 at 17:27

















0















I have written some Python modifying existing code to run through some JSON files and strip out some text. The end result should be saving the now edited file with the same file name "- clean.json" at the end.



I already have the code that loops through the input path, and all JSON files complete. I have also completed and tested that each file does open and the desired text is removed.



The final area is where I need help, how do I now take that updated file and save it back with an updated name. That updated name being -clean.json



All help is appreciated, script is included below:



def normalize_file(file):
global line_number
for line in file:
line_number += 1
json_object = json.loads(line)
logging.info("hi")
if not isinstance(json_object, dict):
logging.error('%s: Top level record must be a dict but was a %s',
line_number, type(json_object))
continue
try:
normalized_dict = normalize_dict(json_object)
except Exception as e:
logging.error('%s: %s', line_number, e)
continue
json.dump(normalized_dict, sys.stdout)
print()
logging.info("Processed %s lines", line_number)

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
logging.info("hi")
input_path = input("Enter path to JSON files->")
json_folder_path = os.path.join(input_path)
json_files = [pos_json for pos_json in os.listdir(input_path) if pos_json.endswith('.json')]
print(json_files)
for index, js in enumerate(json_files):
with open(os.path.join(input_path,js)) as json_file:
normalize_file(json_file)









share|improve this question
























  • Please reduce your code to a minimum, highlighting where exactly you need help. As it is, your code has several issues - it is unclear which one you need help with. For example, you ignore the user input, defying 1. You never open any files, so how do you know that 3 is working? You are loading several JSON from each file in a fragile manner, but your description implies otherwise.

    – MisterMiyagi
    Mar 22 at 16:40







  • 1





    @MisterMiyagi - understood, i have posted my updated code. the main body and function it calls only. I have also simplified the question I believe. Thanks for your feedback.

    – SSISPissesMeOff
    Mar 22 at 17:27













0












0








0








I have written some Python modifying existing code to run through some JSON files and strip out some text. The end result should be saving the now edited file with the same file name "- clean.json" at the end.



I already have the code that loops through the input path, and all JSON files complete. I have also completed and tested that each file does open and the desired text is removed.



The final area is where I need help, how do I now take that updated file and save it back with an updated name. That updated name being -clean.json



All help is appreciated, script is included below:



def normalize_file(file):
global line_number
for line in file:
line_number += 1
json_object = json.loads(line)
logging.info("hi")
if not isinstance(json_object, dict):
logging.error('%s: Top level record must be a dict but was a %s',
line_number, type(json_object))
continue
try:
normalized_dict = normalize_dict(json_object)
except Exception as e:
logging.error('%s: %s', line_number, e)
continue
json.dump(normalized_dict, sys.stdout)
print()
logging.info("Processed %s lines", line_number)

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
logging.info("hi")
input_path = input("Enter path to JSON files->")
json_folder_path = os.path.join(input_path)
json_files = [pos_json for pos_json in os.listdir(input_path) if pos_json.endswith('.json')]
print(json_files)
for index, js in enumerate(json_files):
with open(os.path.join(input_path,js)) as json_file:
normalize_file(json_file)









share|improve this question
















I have written some Python modifying existing code to run through some JSON files and strip out some text. The end result should be saving the now edited file with the same file name "- clean.json" at the end.



I already have the code that loops through the input path, and all JSON files complete. I have also completed and tested that each file does open and the desired text is removed.



The final area is where I need help, how do I now take that updated file and save it back with an updated name. That updated name being -clean.json



All help is appreciated, script is included below:



def normalize_file(file):
global line_number
for line in file:
line_number += 1
json_object = json.loads(line)
logging.info("hi")
if not isinstance(json_object, dict):
logging.error('%s: Top level record must be a dict but was a %s',
line_number, type(json_object))
continue
try:
normalized_dict = normalize_dict(json_object)
except Exception as e:
logging.error('%s: %s', line_number, e)
continue
json.dump(normalized_dict, sys.stdout)
print()
logging.info("Processed %s lines", line_number)

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
logging.info("hi")
input_path = input("Enter path to JSON files->")
json_folder_path = os.path.join(input_path)
json_files = [pos_json for pos_json in os.listdir(input_path) if pos_json.endswith('.json')]
print(json_files)
for index, js in enumerate(json_files):
with open(os.path.join(input_path,js)) as json_file:
normalize_file(json_file)






python json python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 17:26







SSISPissesMeOff

















asked Mar 22 at 16:36









SSISPissesMeOffSSISPissesMeOff

1301214




1301214












  • Please reduce your code to a minimum, highlighting where exactly you need help. As it is, your code has several issues - it is unclear which one you need help with. For example, you ignore the user input, defying 1. You never open any files, so how do you know that 3 is working? You are loading several JSON from each file in a fragile manner, but your description implies otherwise.

    – MisterMiyagi
    Mar 22 at 16:40







  • 1





    @MisterMiyagi - understood, i have posted my updated code. the main body and function it calls only. I have also simplified the question I believe. Thanks for your feedback.

    – SSISPissesMeOff
    Mar 22 at 17:27

















  • Please reduce your code to a minimum, highlighting where exactly you need help. As it is, your code has several issues - it is unclear which one you need help with. For example, you ignore the user input, defying 1. You never open any files, so how do you know that 3 is working? You are loading several JSON from each file in a fragile manner, but your description implies otherwise.

    – MisterMiyagi
    Mar 22 at 16:40







  • 1





    @MisterMiyagi - understood, i have posted my updated code. the main body and function it calls only. I have also simplified the question I believe. Thanks for your feedback.

    – SSISPissesMeOff
    Mar 22 at 17:27
















Please reduce your code to a minimum, highlighting where exactly you need help. As it is, your code has several issues - it is unclear which one you need help with. For example, you ignore the user input, defying 1. You never open any files, so how do you know that 3 is working? You are loading several JSON from each file in a fragile manner, but your description implies otherwise.

– MisterMiyagi
Mar 22 at 16:40






Please reduce your code to a minimum, highlighting where exactly you need help. As it is, your code has several issues - it is unclear which one you need help with. For example, you ignore the user input, defying 1. You never open any files, so how do you know that 3 is working? You are loading several JSON from each file in a fragile manner, but your description implies otherwise.

– MisterMiyagi
Mar 22 at 16:40





1




1





@MisterMiyagi - understood, i have posted my updated code. the main body and function it calls only. I have also simplified the question I believe. Thanks for your feedback.

– SSISPissesMeOff
Mar 22 at 17:27





@MisterMiyagi - understood, i have posted my updated code. the main body and function it calls only. I have also simplified the question I believe. Thanks for your feedback.

– SSISPissesMeOff
Mar 22 at 17:27












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304120%2fstripping-and-renaming-files%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304120%2fstripping-and-renaming-files%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?