Paste Two Text Lists (One List a File) into One List Separated by Semicolon

Paste two text lists (one list a file) into one list separated by semicolon

paste -d';' File1 File2  >  File3

Assemble multiple lists into a single list through a loop

you can add list and append list in the list with list +=

with open('my_csv_file.csv') as csvfile:
res = []
reader = csv.reader(csvfile, delimiter=';')

for row in reader:
if row != '' and row[0].isdigit():
res += [[row[0], row[1], row[2], row[3], row[4]]]


or you can use append

with open('my_csv_file.csv') as csvfile:
res = []
reader = csv.reader(csvfile, delimiter=';')

for row in reader:
if row != '' and row[0].isdigit():
res.append([row[0], row[1], row[2], row[3], row[4]])

Python - Read in Comma Separated File, Create Two lists

let's keep it very simple.

list1 = []
list2 = []

with open ("mid.dat") as myfile:
for line in myfile:
x1,x2 = map(float,line.split(','))
list1.append(x1)
list2.append(x2)

print(list1)
print(list2)

combine two comma seperated txt file file in python

ASSUMING the two files have the same number of lines, this will do it:

fileLake = open("lakes.txt", "r")  # has two columns
filePortage = open("portages.txt", "r") # has three columns

for line1,line2 in zip(fileLake.readlines(), filePortage.readlines()):
lake = line1.strip().split(",")
port = line2.strip().split(",")
print( ",".join(lake + port) )

Read text files with multiple lists with spacings and commas exist between elements in the lists into pandas dataframe

You can use read_csv, by specifying a separator which will not occur in the lines (e.g. \0) (so that each line will be read as a whole) and ast.literal_eval as a converter for the values:

import ast

pd.read_csv('tropical.txt', header=None, sep='\0', names=['fruits'], converters={ 'fruits' : ast.literal_eval })

Output:

                         fruits
0 [papaya, mangosteen, banana]
1 []
2 [coconut, mango]
3 [mangosteen, papaya]

split a list read in from file at the commas into a list of seperate elements

Once the list is built (or directly with the file handle as l, there's no need to store the list first) I would just rstrip and split in a list comprehension like this:

l = ['001,Joe,Bloggs,Test1:99,Test2:100,Test3:33\n', '002,Ash,Smith,Test1:22,Test2:63,Test3:99\n']

newl = [v.rstrip().split(",") for v in l]

print(newl)

result:

[['001', 'Joe', 'Bloggs', 'Test1:99', 'Test2:100', 'Test3:33'], ['002', 'Ash', 'Smith', 'Test1:22', 'Test2:63', 'Test3:99']]

for a flat list do a double loop instead (or use itertools.chain.from_iterable, well there are a lot of ways to do that):

newl = [x for v in l for x in v.rstrip().split(",")]

without listcomp (just for "readability" when you're not used to listcomps, after that, switch to listcomps :)):

newl = []
for v in l:
newl.append(v.rstrip().split(","))

(use extend instead of append to get a flat list)

of course I always forget to mention csv which has default separator as comma and strips the newlines:

import csv
newl = list(csv.reader(l))

flat (using itertools this time):

newl = list(itertools.chain.from_iterable(csv.reader(l)))

(l can be a file handle or a list of lines for the csv module)

How to merge 2 CSV files based on filename

try this:

paste -d, 1234ABC.stats.csv 1234ABC.csv 

loop over multiple files in local directory

#!/bin/bash
for statsfile in *.stats.csv; do
paste -d, "$statsfile" "${statsfile//.stats/}" > "new_${statsfile//.stats/}"
done

String of values separated by commas or semicolons into a Python list

In this case I whould use the re module

>>> import re
>>>
>>> data = "billg@microsoft.com;steve@apple.com; dhh@37signals.com"
>>> stuff = re.split(r"\s*[,;]\s*", data.strip())


Related Topics



Leave a reply



Submit