fix_csv: bonus 2
This commit is contained in:
parent
aa060e4c33
commit
b0407c867a
|
|
@ -6,9 +6,15 @@ import csv
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
def normalise(input, output, in_delimiter="|", in_quote='"'):
|
def normalise(input, output, in_delimiter=None, in_quote=None):
|
||||||
"""Normalise `input` CSV file into `output` file."""
|
"""Normalise `input` CSV file into `output` file."""
|
||||||
Path(output).touch(exist_ok=True)
|
Path(output).touch(exist_ok=True)
|
||||||
|
with Path(input).open("r", newline="") as in_file:
|
||||||
|
dialect = csv.Sniffer().sniff(in_file.read())
|
||||||
|
if in_delimiter is None:
|
||||||
|
in_delimiter = dialect.delimiter
|
||||||
|
if in_quote is None:
|
||||||
|
in_quote = dialect.quotechar
|
||||||
with Path(input).open("r", newline="") as in_file, Path(output).open(
|
with Path(input).open("r", newline="") as in_file, Path(output).open(
|
||||||
"w"
|
"w"
|
||||||
) as out_file:
|
) as out_file:
|
||||||
|
|
@ -33,8 +39,8 @@ if __name__ == "__main__":
|
||||||
parser.add_argument("input")
|
parser.add_argument("input")
|
||||||
parser.add_argument("output")
|
parser.add_argument("output")
|
||||||
|
|
||||||
parser.add_argument("--in-delimiter", dest="in_delimiter", default="|")
|
parser.add_argument("--in-delimiter", dest="in_delimiter", default=None)
|
||||||
parser.add_argument("--in-quote", dest="in_quote", default='"')
|
parser.add_argument("--in-quote", dest="in_quote", default=None)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue