Skip to main content
deleted 217 characters in body
Source Link
UpAndAdam
  • 1.7k
  • 1
  • 4
  • 20

Success!

I managed to solve this in the end both by including the write loop inside of the read loop, also by including the delimiter and fieldnames parameters along with the if statement to check for fieldnames being recognised as data.

That way, there was no need to use an 'else:' clause.

Use of icecream for debugging was invaluable!

with open(sys.argv[1]) as before, open(sys.argv[2], "w") as after:   reader = csv.DictReader(   before, fieldnames=["name", "house"], delimiter=","   )   writer = csv.DictWriter(after, fieldnames=["first", "last", "house"])   # This line writes the header row to the CSV file, based on the field names provided earlier   writer.writeheader()   for row in reader:   # This 'if' statement checks for fieldnames being recognised as data   if row == {"house": "house", "name": "name"}:   continue   last, first = row["name"].split(",")   new_format = {   "first"first": first.lstrip(),   "last": last,   "house": row["house"],   }   # This line writes the content of the new_format dictionary as a row in the after CSV file   writer.writerow(new_format) 

Success!

I managed to solve this in the end both by including the write loop inside of the read loop, also by including the delimiter and fieldnames parameters along with the if statement to check for fieldnames being recognised as data.

That way, there was no need to use an 'else:' clause.

Use of icecream for debugging was invaluable!

with open(sys.argv[1]) as before, open(sys.argv[2], "w") as after:   reader = csv.DictReader(   before, fieldnames=["name", "house"], delimiter=","   )   writer = csv.DictWriter(after, fieldnames=["first", "last", "house"])   # This line writes the header row to the CSV file, based on the field names provided earlier   writer.writeheader()   for row in reader:   # This 'if' statement checks for fieldnames being recognised as data   if row == {"house": "house", "name": "name"}:   continue   last, first = row["name"].split(",")   new_format = {   "first": first.lstrip(),   "last": last,   "house": row["house"],   }   # This line writes the content of the new_format dictionary as a row in the after CSV file   writer.writerow(new_format) 

Success!

I managed to solve this in the end both by including the write loop inside of the read loop, also by including the delimiter and fieldnames parameters along with the if statement to check for fieldnames being recognised as data.

That way, there was no need to use an 'else:' clause.

Use of icecream for debugging was invaluable!

with open(sys.argv[1]) as before, open(sys.argv[2], "w") as after: reader = csv.DictReader( before, fieldnames=["name", "house"], delimiter="," ) writer = csv.DictWriter(after, fieldnames=["first", "last", "house"]) # This line writes the header row to the CSV file, based on the field names provided earlier writer.writeheader() for row in reader: # This 'if' statement checks for fieldnames being recognised as data if row == {"house": "house", "name": "name"}: continue last, first = row["name"].split(",") new_format = { first": first.lstrip(), "last": last, "house": row["house"], } # This line writes the content of the new_format dictionary as a row in the after CSV file writer.writerow(new_format) 
Use of fieldnames and check. Combining write loop inside of the read loop.
Source Link

Success!

I managed to solve this in the end both by including the write loop inside of the read loop, also by including the delimiter and fieldnames parameters along with the if statement to check for fieldnames being recognised as data.

That way, there was no need to use an 'else:' clause.

Use of icecream for debugging was invaluable!

with open(sys.argv[1]) as before, open(sys.argv[2], "w") as after: reader = csv.DictReader( before, fieldnames=["name", "house"], delimiter="," ) writer = csv.DictWriter(after, fieldnames=["first", "last", "house"]) # This line writes the header row to the CSV file, based on the field names provided earlier writer.writeheader() for row in reader: # This 'if' statement checks for fieldnames being recognised as data if row == {"house": "house", "name": "name"}: continue last, first = row["name"].split(",") new_format = { "first": first.lstrip(), "last": last, "house": row["house"], } # This line writes the content of the new_format dictionary as a row in the after CSV file writer.writerow(new_format) 

Success!

I managed to solve this in the end both by including the write loop inside of the read loop, also by including the delimiter and fieldnames parameters along with the if statement to check for fieldnames being recognised as data.

Use of icecream for debugging was invaluable!

with open(sys.argv[1]) as before, open(sys.argv[2], "w") as after: reader = csv.DictReader( before, fieldnames=["name", "house"], delimiter="," ) writer = csv.DictWriter(after, fieldnames=["first", "last", "house"]) # This line writes the header row to the CSV file, based on the field names provided earlier writer.writeheader() for row in reader: # This 'if' statement checks for fieldnames being recognised as data if row == {"house": "house", "name": "name"}: continue last, first = row["name"].split(",") new_format = { "first": first.lstrip(), "last": last, "house": row["house"], } # This line writes the content of the new_format dictionary as a row in the after CSV file writer.writerow(new_format) 

Success!

I managed to solve this in the end both by including the write loop inside of the read loop, also by including the delimiter and fieldnames parameters along with the if statement to check for fieldnames being recognised as data.

That way, there was no need to use an 'else:' clause.

Use of icecream for debugging was invaluable!

with open(sys.argv[1]) as before, open(sys.argv[2], "w") as after: reader = csv.DictReader( before, fieldnames=["name", "house"], delimiter="," ) writer = csv.DictWriter(after, fieldnames=["first", "last", "house"]) # This line writes the header row to the CSV file, based on the field names provided earlier writer.writeheader() for row in reader: # This 'if' statement checks for fieldnames being recognised as data if row == {"house": "house", "name": "name"}: continue last, first = row["name"].split(",") new_format = { "first": first.lstrip(), "last": last, "house": row["house"], } # This line writes the content of the new_format dictionary as a row in the after CSV file writer.writerow(new_format) 
Source Link

Success!

I managed to solve this in the end both by including the write loop inside of the read loop, also by including the delimiter and fieldnames parameters along with the if statement to check for fieldnames being recognised as data.

Use of icecream for debugging was invaluable!

with open(sys.argv[1]) as before, open(sys.argv[2], "w") as after: reader = csv.DictReader( before, fieldnames=["name", "house"], delimiter="," ) writer = csv.DictWriter(after, fieldnames=["first", "last", "house"]) # This line writes the header row to the CSV file, based on the field names provided earlier writer.writeheader() for row in reader: # This 'if' statement checks for fieldnames being recognised as data if row == {"house": "house", "name": "name"}: continue last, first = row["name"].split(",") new_format = { "first": first.lstrip(), "last": last, "house": row["house"], } # This line writes the content of the new_format dictionary as a row in the after CSV file writer.writerow(new_format)