text-files

Convert tab-delimited txt file into a csv file using Python

末鹿安然 提交于 2019-11-26 10:56:07
问题 So I want to convert a simple tab delimited text file into a csv file. If I convert the txt file into a string using string.split(\'\\n\') I get a list with each list item as a string with \'\\t\' between each column. I was thinking I could just replace the \'\\t\' with a comma but it won\'t treat the string within the list like string and allow me to use string.replace. Here is start of my code that still needs a way to parse the tab \"\\t\". import csv import sys txt_file = r\"mytxt.txt\"

In C, how should I read a text file and print all strings

不问归期 提交于 2019-11-26 10:15:44
I have a text file named test.txt I want to write a C program that can read this file and print the content to the console (assume the file contains only ASCII text). I don't know how to get the size of my string variable. Like this: char str[999]; FILE * file; file = fopen( "test.txt" , "r"); if (file) { while (fscanf(file, "%s", str)!=EOF) printf("%s",str); fclose(file); } The size 999 doesn't work because the string returned by fscanf can be larger than that. How can I solve this? The simplest way is to read a character, and print it right after reading: int c; FILE *file; file = fopen(

Create a .txt file if doesn't exist, and if it does append a new line

拟墨画扇 提交于 2019-11-26 09:17:01
问题 I would like to create a .txt file and write to it, and if the file already exists I just want to append some more lines: string path = @\"E:\\AppServ\\Example.txt\"; if (!File.Exists(path)) { File.Create(path); TextWriter tw = new StreamWriter(path); tw.WriteLine(\"The very first line!\"); tw.Close(); } else if (File.Exists(path)) { TextWriter tw = new StreamWriter(path); tw.WriteLine(\"The next line!\"); tw.Close(); } But the first line seems to always get overwritten... how can I avoid

How do I correct the character encoding of a file?

懵懂的女人 提交于 2019-11-26 09:09:24
问题 I have an ANSI encoded text file that should not have been encoded as ANSI as there were accented characters that ANSI does not support. I would rather work with UTF-8. Can the data be decoded correctly or is it lost in transcoding? What tools could I use? Here is a sample of what I have: ç é I can tell from context (café should be café) that these should be these two characters: ç é 回答1: EDIT: A simple possibility to eliminate before getting into more complicated solutions: have you tried

Batch / Find And Edit Lines in TXT file

社会主义新天地 提交于 2019-11-26 09:08:50
问题 I want to create a batch while which finds specific lines in a batch file and are able to edit these lines. Example: //TXT FILE// ex1 ex2 ex3 ex4 i want to let the batch file find \'ex3\' and edit this to \'ex5\' to let it look like this: ex1 ex2 ex5 ex4 回答1: On a native Windows install, you can either use batch(cmd.exe) or vbscript without the need to get external tools. Here's an example in vbscript: Set objFS = CreateObject("Scripting.FileSystemObject") strFile = "c:\test\file.txt" Set

Java - delete line from text file by overwriting while reading it

安稳与你 提交于 2019-11-26 07:49:06
问题 I\'m trying to delete a line of text from a text file without copying to a temporary file. I am trying to do this by using a Printwriter and a Scanner and having them traverse the file at the same time, the writer writing what the Scanner reads and overwriting each line with the same thing, until it gets to the line that I wish to delete. Then, I advance the Scanner but not the writer, and continue as before. Here is the code: But first, the parameters: My file names are numbers, so this

Java - How Can I Write My ArrayList to a file, and Read (load) that file to the original ArrayList?

只谈情不闲聊 提交于 2019-11-26 07:31:29
问题 I am writing a program in Java which displays a range of afterschool clubs (E.G. Football, Hockey - entered by user). The clubs are added into the following ArrayList : private ArrayList<Club> clubs = new ArrayList<Club>(); By the followng Method: public void addClub(String clubName) { Club club = findClub(clubName); if (club == null) clubs.add(new Club(clubName)); } \'Club\' is a class with a constructor - name: public class Club { private String name; public Club(String name) { this.name =

How to read a text file directly from Internet using Java?

你说的曾经没有我的故事 提交于 2019-11-26 06:46:10
问题 I am trying to read some words from an online text file. I tried doing something like this File file = new File(\"http://www.puzzlers.org/pub/wordlists/pocket.txt\"); Scanner scan = new Scanner(file); but it didn\'t work, I am getting http://www.puzzlers.org/pub/wordlists/pocket.txt as the output and I just want to get all the words. I know they taught me this back in the day but I don\'t remember exactly how to do it now, any help is greatly appreciated. 回答1: Use an URL instead of File for

How to import data from text file to mysql database

断了今生、忘了曾经 提交于 2019-11-26 06:16:56
问题 I have a 350MB file named text_file.txt containing this tab delimited data: 345868230 1646198120 1531283146 Keyword_1531283146 1.55 252910000 745345566 1646198120 1539847239 another_1531276364 2.75 987831000 ... MySQL Database name: Xml_Date Database table: PerformanceReport I have already created the table with all the destination fields. I want to import this text file data into a MySQL. I googled and found some commands like LOAD DATA INFILE and quite confused on how to use it. How can I

Convert 12-hour date/time to 24-hour date/time

筅森魡賤 提交于 2019-11-26 05:38:27
问题 I have a tab delimited file where each record has a timestamp field in 12-hour format: mm/dd/yyyy hh:mm:ss [AM|PM]. I need to quickly convert these fields to 24-hour time: mm/dd/yyyy HH:mm:ss. What would be the best way to do this? I\'m running on a Windows platform, but I have access to sed, awk, perl, python, and tcl in addition to the usual Windows tools. 回答1: Using Perl and hand-crafted regexes instead of facilities like strptime: #!/bin/perl -w while (<>) { # for date times that don't